Bob Gatto
Bob Gatto

Reputation: 95

How do I set a text value of a label in a content page from a view model class?

I'm working on an app where the initial content page has a label control looking like this:

            <Label FontSize="Small"
                   TextColor="White"
                   Margin="10,0,0,0"
                   x:Name="lblOwner"
                   Text="{Binding ownrName,Mode=TwoWay}"/>

But to initialize the app, a database has to be selected first. From the menu, I use the line Command={Binding SelNewDbCommand} This is what the view model looks like:

    public partial class PgViewModel : ObservableValidator
    {
        public SQLiteAsyncConnection ConnStr {  get; set; }
        public ObservableCollection<DbSelect> Dbs { get; set; } = new ObservableCollection<DbSelect>();
        public ObservableCollection<Groups> Grps { get; set; } = new ObservableCollection<Groups>();
        public bool grpVisible {  get; set; }
        public string ownrName { get; set; }

        LakFuncs laks;
        public PgViewModel() 
        {
            laks = new LakFuncs();
        }

        [RelayCommand]
        public async void SelNewDb()
        {
            var dbList = laks.GetDbs();  // This gets a list of all database locations and their full path.

            if (dbList.Count == 0)
            {
                await Shell.Current.DisplayAlert("Note:", "No password databases were found.", "OK");
                return;
            }

            var popup = new SelectDb(dbList);
            await MopupService.Instance.PushAsync(popup);

            var rvalue = await popup.PopupDismissedTask;
            if (rvalue != null)
            {
                var Options = new SQLiteConnectionString(rvalue, true);
                ConnStr = new SQLiteAsyncConnection(Options);

                var grps = await ConnStr.Table<Groups>().ToListAsync();
                Grps.Clear();
                foreach (var grp in grps)
                {
                    Grps.Add(new Groups() { GrpName = grp.GrpName });
                }

                grpVisible = (Grps.Count > 0);
            }

            var query = ConnStr.Table<LakSettings>().Where(x => x.Id == 1);
            LakSettings settings = query.FirstOrDefaultAsync().Result;

            ownrName = settings.DbOwner;  // This is the public variable bound to the Owner's Name in the content page

        }
    }

My situation is everything works except the Owner Name.

I placed a pause dot on the line of code that sets a value to ownrName. I then ran the program, whent through the actions to select a database, and when it paused on that line, I checked the value of settings.DbOwner, and there was a value in it.

What I am looking for is that value to show on the content page while still keeping the MVVM structure. What am I doing wrong?

Upvotes: 1

Views: 42

Answers (0)

Related Questions