[英]WPF - Update Datagrid in ObservableCollection
My datagrid view doesn't update when the source is modified. 修改源时,我的数据网格视图不会更新。
In my View.xaml : 在我的View.xaml中:
<DataGrid IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectedItem="{Binding SelectedItem}">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Width="*" Binding="{Binding ProductHistoryInOutDate}" />
<DataGridTextColumn Header="Quantité" Width="*" Binding="{Binding ProductHistoryQuantity}" />
</DataGrid.Columns>
</DataGrid>
In my ViewModel.cs : 在我的ViewModel.cs中:
private ObservableCollection<ProductHistory> _Items;
public ObservableCollection<ProductHistory> Items
{
get { return _Items; }
set
{
_Items = value;
RaisePropertyChanged("Items");
}
}
[Edit] [编辑]
Command: 命令:
public RelayCommand Remove
{
get
{
if (_Remove == null)
{
_Remove = new RelayCommand(
() => { _UOF.ProductHistoryRepository.Delete(this.SelectedItem);_UOF.Commit(); },
() => SelectedItem != null);
}
return _Remove;
}
}
Remove button: 删除按钮:
<Button Content="Delete" Command="{Binding Path=Remove}" />
The only explanation I can think of is that you are missing Items.Remove(this.SelectedItem)
in your command execute code. 我能想到的唯一解释是你在命令执行代码中缺少Items.Remove(this.SelectedItem)
。
I tried making and example from your code and it worked fine if I added this, there was no issue with observable collection. 我尝试从你的代码制作和示例,如果我添加它,它工作正常,observable集合没有问题。
Since I don't have your code for RelayCommand and RaisePropertyChanged(), I had to manually implement it to try it out, but I assume it is from some library and it works fine. 由于我没有RelayCommand和RaisePropertyChanged()的代码,我不得不手动实现它来尝试它,但我认为它来自某个库,它工作正常。
Here is what it should look like: 这是它应该是什么样子:
public RelayCommand Remove
{
get
{
if (_Remove == null)
{
_Remove = new RelayCommand(
() => { Items.Remove(this.SelectedItem); _UOF.ProductHistoryRepository.Delete(this.SelectedItem); _UOF.Commit(); },
() => SelectedItem != null);
}
return _Remove;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.