INotifyPropertyChanged in WPF

后端 未结 2 1790
梦毁少年i
梦毁少年i 2021-01-19 21:39

Try to understand WPF. This is my test classes:

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ObservableCollection

        
相关标签:
2条回答
  • 2021-01-19 21:58

    The problem is that you are directly setting the original list onto the Window.DataContext, so nothing ever listens to the windows' PropertyChanged event.

    To solve this, set the DataContext to the window itself:

    this.DataContext = this;
    

    and then change the Binding so refer to the property:

    <ComboBox ItemsSource="{Binding MyList}" />
    

    You will also need to change your property definition so that it raises the name of the property being changed, not the name of the member:

    this.RaisePropertyChanged("MyList");
    
    0 讨论(0)
  • 2021-01-19 22:03

    I think you have two problems:

    1) binding should be: {Binding MyList}

    2) on MyList setter you should use RaisePropertyChanged("MyList");

    0 讨论(0)
提交回复
热议问题