下面是有问题的代码
private ObservableCollection<Params> _values; public MainWindow() { InitializeComponent(); this.dgrid1.ItemsSource = _values; } private void btnUploadClick(object sender, RoutedEventArgs e) { _values = new ObservableCollection<Params> { new AirtightParam { DeviceNumber = 1, ProgramNumber = 2, TestType = 3, }, }; }
原因分析:在窗口构造时给datagrid控件设置一个数据源_values,但实际上是null的。在按钮点击时给数据源实例化,实际并不是_values的属性值发生了变化,所以不会去通知UI变更,只能对构造时的_values进行操作清空及Add操作才会通知。
更新代码:
private ObservableCollection<Params> _values; public MainWindow() { InitializeComponent(); _values = new ObservableCollection<Params>(); this.dgrid1.ItemsSource = _values; } private void btnUploadClick(object sender, RoutedEventArgs e) { _values.Clear(); _values.Add(new AirtightParam { DeviceNumber = 1, ProgramNumber = 2, TestType = 3, }); }
标签:ObservableCollection,数据源,绑定,private,values,new,UI,WPF From: https://www.cnblogs.com/duju/p/17516113.html