WPF DataGrid not updating on PropertyChanged

前端 未结 3 817
离开以前
离开以前 2021-01-11 11:27

i\'ve a problem updating my datagrid when clicking the button by using NotifyPropertyChanged. It works if i set the DataGrid.ItemsSource in code behind, but it doesn\'t if i

相关标签:
3条回答
  • 2021-01-11 12:04

    When you go crazy (like me) because it doesn't work this time. Check the order of the calls. The OnPropertyChanged() must be called after the value was assigned.

    public string Property1
    { 
        get { return _field1; } 
        set 
        {
            // assign the value first
            _field1 = value;
    
            // then call the property changed
            OnPropertyChanged("Property1");
        }
    }
    
    0 讨论(0)
  • 2021-01-11 12:06

    In your codebehind .xaml.cs create property

       public ObservableCollection<SampleClass> MyCollection {get; set;}
    
       private void Window_Loaded(object sender, RoutedEventArgs e)
       {
        //if i set the ItemsSource here, updating of the UI works
          dataGrid1.ItemsSource = MyCollection;
       }
    

    In XAML:

       <DataGrid ItemsSource="{Binding Path=., Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
    
    0 讨论(0)
  • 2021-01-11 12:15

    I followed up on syned's tip. For me I had to us the Mode as

    Mode=TwoWay
    

    The key here was the

    UpdateSourceTrigger
    

    property.

    Thanks a lot...!

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