How to notify update of data members when using a command? - databinding (MVVM Toolkit, WPF, C#)

Stephen Lord 1 Reputation point
2022-01-11T17:37:32.337+00:00

Hello,

I am using MVVM Toolkit in a WPF C# application.
I cannot seem to find a solution to the following problem in relation to databinding and notifying of updates.

In the code below I have a button procedure OnBtnAddSetup, this button will eventually load data from a database and display it in a set of text boxes and labels.
At the moment it manually sets the data within the data members for testing. It updates them however it does not update the fields they are bound to.

In relation to Microsoft MVVM toolkit how should this be done?
I have tried many methods on the internet regarding INotifyPropertyChanged and ObservableObject but have not found any to work as yet.

The binding seems to work two ways except in the case when update from the button command so I think I am sure I am missing some code in the button to notify setupA has been updated.

Thank you for any help whatsoever.

CarSetup Class

using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H.SUT.Models
{
    // CarSetup Definition
    public class CarSetup // : ObservableObject
    {
        // CarSetup Type Strings V1 defined for use by the class

        public int year { get; set; }

        public string championship { get; set; }

        // 786 more ..........

    }
}

ViewModel

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using H.SUT.Models;

namespace H.SUT.ViewModels
{
    public class SetupViewModel : ObservableObject
    {

        public SetupViewModel()
        {
        }

        private CarSetup setupA = new CarSetup();
        public CarSetup SetupA
        {
            get => setupA;
            set => SetProperty(ref setupA, value);
        }


        private ICommand _btnAddSetup;

        public ICommand BtnAddSetupCommand => _btnAddSetup ?? (_btnAddSetup = new RelayCommand(OnBtnAddSetup));

        public void OnBtnAddSetup()
        {

            //Example Data
            //INFO

            setupA.championship = "F2";
            setupA.year = 2022;

            // 782 more

        }
    }

View (just the binding)

<TextBox Text="{Binding SetupA.championship , Mode=TwoWay}" Grid.Column="0" Grid.Row="0" Style="{StaticResource Setup_TextBox}" />
<TextBox Text="{Binding SetupA.year, Mode=TwoWay}" Grid.Column="1" Grid.Row="0" Style="{StaticResource Setup_TextBox}" />
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2022-01-11T20:45:20.05+00:00

    You need to make CarSetup inherit from ObservableObject (you commented it out) so you can call SetProperty after a value is updated like you did for setupA. Looks like you will have to do it for a lot of properties.

         private CarSetup setupA = new CarSetup();
         public CarSetup SetupA
         {
             get => setupA;
             set => SetProperty(ref setupA, value);
         }
    

    Another option is to use something like post sharp to automatically add the notify property changed

    https://www.syncfusion.com/blogs/post/how-to-get-rid-of-the-inotifypropertychanged-boilerplate.aspx

    0 comments No comments

  2. Stephen Lord 1 Reputation point
    2022-01-17T09:54:35.717+00:00

    Hi,

    Thank you for your reply to my question.
    Sorry for the delay, I have just had a chance to revisit this project this morning.

    I tried to uncomment the ObservableObject in my CarSetup class but this gives an error in the code and will not run ( public class CarSetup : ObservableObject ) and I wasn't sure how to proceed with this.

    I have found a solution that works, not sure if it is the correct method.
    I added the following line of code into my command button procedure after assigning the new values.
    This then triggers the View to update it's display and makes it work how I want. I should now be able to load data from the database and then update the view using this.

    OnPropertyChanged("SetupA");
    

    Thank you
    Best Regards
    Stephen


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.