1

I have a view model class in a folder called ViewModel. The file is called PgViewMode.cs and looks like this:

using CommunityToolkit.Mvvm.ComponentModel;
using LockAndKeyMaui.Models;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using SQLite;
using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Maui.Storage;
using Mopups.Services;

namespace LockAndKeyMaui.ViewModel
{
    public partial class PgViewModel : ObservableValidator
    {
        public ObservableCollection<Groups> Grps { get; set; } = new ObservableCollection<Groups>();

        public PgViewModel()
        {
            Grps = new ObservableCollection<Groups>();
        }
     }
}

In one of my view files, I have the XAML looking like this:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sf="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModel"
             x:DataType="viewmodel:PgViewModel"
             x:Class="LockAndKeyMaui.View.PasswdInfo"
             BackgroundColor="DarkGreen">

And in the code-behind of this view file the constructor looks like this:

public PasswdInfo()
{
    laks = new LakFuncs();
    InitializeComponent();
    BindingContext = new PgViewModel();
}

Now at one point in the code-behind of the view, I have:

Grps.Clear();
foreach (var grp in grps)
{
    Grps.Add(new Groups() { GrpName = grp.GrpName });
}

grplist.ItemsSource = Grps;
grplist.IsVisible = (Grps.Count > 0);

And this is where I get the error message: "The name 'Grps' does not exist in the current context." Grps is the definition of the ObservableCollection<Groups> in the view model file. I realize it is a different file name, but shouldn't it be recognized in some way?

5
  • 1
    "And this is where I get the errors" is not so helpful in understanding the problem. Please post the error message. Commented May 20, 2024 at 3:27
  • 1
    You should read How to Ask and then give us a minimal reproducible example. Commented May 20, 2024 at 4:28
  • Grpsis not a property of the view but of the viewmodel. The Bindingcontext is implicitly used from within XAML, but not from the code behind. So you should replace (e.g.) Grps.Clear(); with BindingContext.Grps.Clear(); Commented May 20, 2024 at 5:02
  • @JohanDonne The second part of your comment is wrong. It's not possible to directly access Grps from the BindingContext like that. To do this, a cast is required, e.g. : (BindingContext as PgViewModel).Grps.Clear();. However, I would discourage to do so and use a reference to the ViewModel instead. Commented May 20, 2024 at 7:52
  • @Julian My bad, forgot about the cast, Thanks for the correction. Commented May 20, 2024 at 13:04

1 Answer 1

1

You need to store a reference to your PgViewModel in the code-behind of your PasswdInfo page:

public partial class PasswdInfo : ContentPage
{
    private PgViewModel _vm;

    public PasswdInfo()
    {
          InitializeComponent();
          BindingContext = _vm = new PgViewModel();
    }

    //...
}

Then you can access any of the public properties directly:

_vm.Grps.Clear();
foreach (var grp in grps)
{
    _vm.Grps.Add(new Groups() { GrpName = grp.GrpName });
}

grplist.ItemsSource = _vm.Grps;
grplist.IsVisible = _vm.Grps.Count > 0;

This is necessary, because the ViewModel is not in any way directly related to the View. If you always update everything from within the code-behind, you might not even need to set the BindingContext, but that depends on whether you use any binding expressions in your XAML.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.