How can I programmatically do what pressing Delete does when an element in a column of a row is clicked? It's not intuitive to our customer that you select the row and press the Delete key, so they want a cell in each row which they click to delete the row. We want it to do the same thing as pressing Delete (same prompting, same removal of record).
There are a few ways to set this up. Here's one way, which involves removing the data item from the data source. Note, for this to work, the data source must implement INotifyCollectionChanged so that the grid is able to detect the item removal.
<igDP:UnboundField Name="Delete"> <igDP:Field.Settings> <igDP:FieldSettings> <igDP:FieldSettings.CellValuePresenterStyle> <Style TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Button Click="OnDeleteButtonClick" HorizontalAlignment="Center">X</Button> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:FieldSettings.CellValuePresenterStyle> </igDP:FieldSettings> </igDP:Field.Settings></igDP:UnboundField>
In the code-behind...
private void OnDeleteButtonClick(object sender, RoutedEventArgs e){ Button b = sender as Button; if (b == null) return; DataRecord dr = b.DataContext as DataRecord; if (dr == null) return; ObservableCollection<Task> dataSource = this.xamDG.DataSource as ObservableCollection<Task>; if (dataSource == null) return; dataSource.Remove(dr.DataItem as Task);}
Great example -- infragistics should publish this as I'll bet it's a very common question. It took me hours to find this from digging through help documentation, forums, etc. Thanks for the great post!!
I agree, great example. I printed this out and it's going to live in my project notebook.
Since you like this, you can take a look at another approach, which will take care of some of the styling issues when putting a plain button in a field. It also demonstrates the use of more complex code:
http://blogs.infragistics.com/blogs/alex_fidanov/archive/2009/07/14/shortcut-delete-button-on-xamdatagrid.aspx