I have a setting that looks like this:
// myDG is a DataGrid whose columns are DataGridTextColumn ObservableCollection<MyItem> myOC; // myOC is populated with some new MyItem myDG.ItemsSource = myOC;
where MyItem implements INotifyPropertyChanged . How to correctly catch when the user enters a value in a cell?
I tried to catch PropertyChanged on MyItem s, but I also periodically update the values ββin the background (the idea is that when the user manually edits the value, a flag is triggered that reports periodic calculation to avoid overwriting manually entered data). Thus, PropertyChanged catches everything, including periodic updates, which I do not want. I believe it is possible to do this (by setting a flag when I do a periodic calculation, and then checking if the flag is missing in the PropertyChanged event handler, but I want to know if there is a simpler solution.)
I tried catching myDG.CurrentCellChanged , but it worked every time the user changed the cell selection, and not specifically when editing the contents of the cells.
Edit: Here is the XAML:
<DataGrid x:Name="myDG" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader"> <DataGrid.Columns> <DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" IsReadOnly="True"/> <DataGridTextColumn Header="Col2" Binding="{Binding Prop2}" IsReadOnly="False"/> </DataGrid.Columns> </DataGrid>
Here is the implementation of MyItem (uses Fody / PropertyChanged ):
[ImplementPropertyChanged] class MyItem : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public string Prop1 { get; set; } public string Prop2 { get; set; } public MyItem() { Prop1 = Prop2 = ""; } }
c # wpf datagrid
jcai
source share