The value of the modified value of the WPF DataGrid cell

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 = ""; } } 
+17
c # wpf datagrid
source share
2 answers

The solution was to catch the CellEditEnding event.

 // In initialization myDG.CellEditEnding += myDG_CellEditEnding; void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { var column = e.Column as DataGridBoundColumn; if (column != null) { var bindingPath = (column.Binding as Binding).Path.Path; if (bindingPath == "Col2") { int rowIndex = e.Row.GetIndex(); var el = e.EditingElement as TextBox; // rowIndex has the row index // bindingPath has the column binding // el.Text has the new, user-entered value } } } } 
+28
source share

You can achieve this with the CellEditEnding event, and some attributes need to be added to the DataGridTextColumn as shown below: -

 <DataGrid x:Name="myDG" CellEditEnding="myDG_CellEditEnding" 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 x:Name="dataGridTextColumn"Header="Col2" Binding="{Binding Prop2, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" Width="*" /> </DataGrid.Columns> </DataGrid> 

IN C #

  private void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { string prop1 = (e.Row.Item as DataRowView).Row[1].ToString(); } 
0
source share

All Articles