Search code examples
.netwpfmvvmdata-bindinginotifypropertychanged

INotifyPropertyChanged PropertyChanged event remains null


I have searched on the forum and some other sources regarding PropertyChanged event remains null but didn't find any solution. I had created a sample WPF app with AModel ,AViewModel and MainWindow. Model and ViewModel has implemented INotifyPropertyChanged interface but when checked at runtime both PropertyChanged events remains null. Application Code is as below:

public class AModel : INotifyPropertyChanged
{
    public AModel()
    {

    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyname = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }
    private string _A;
    public string A
    {
        get
        {
            return _A;
        }
        set
        {
            _A = value;
            NotifyPropertyChanged("A");
        }
    }
}

public class AViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyname = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
        }
    }
    public AViewModel()
    {
        MyList = new List<AModel>();

    }

    private List<AModel> _MyList;
    public List<AModel> MyList
    {
        get
        {
            return _MyList;
        }
        set
        {
            _MyList = value;
        }
    }


}

public partial class MainWindow : Window
{
    private AViewModel _objViewModel=new AViewModel();
    public AViewModel ObjViewModel
    {
        get
        {
            return _objViewModel;
        }
        set
        {
            _objViewModel = value;
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        ObjViewModel = new AViewModel();
        DataContext = ObjViewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ObjViewModel.MyList.Add(new AModel() { A = "Sample Text" });
    }
}

Now when i add breakpoint to my model class PropertyChanged event it is always null. I had created object of view model and assigned that to the DataContext of MainWindow but still its null. I am not able to understand where I am going wrong in this simple application. I had checked binding which is working properly.

Thanks in Advance


Solution

  • PropertyChanged event is null here because the property is not actually getting changed. You are creating a collection and then binding it to an UI control. If you want to see a property changed getting triggered, then you can try this :-

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <DataGrid Margin="20" AutoGenerateColumns="True" ItemsSource="{Binding MyList}"/>
            <ListBox Grid.Row="1" ItemsSource="{Binding MyList}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button  Grid.Row="2" HorizontalAlignment="Center" Margin="20" Content="Click Me" Click="Button_Click"/>
        </Grid>
    

    So, when you will try to update the item in the textbox :-

    <DataTemplate>
                        <TextBox Text="{Binding A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </DataTemplate>
    

    then you will notice that the property changed is being triggered as soon as you type in the TextBox:-

    {Method = {Void OnPropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)}}
    

    Please comment if you need more clarification.