Pass DataGrid row to code behind when a member of the row is modified
I have a DataGrid that gets the Name, Age, and IsFriend values from a
List. I formatted my columns this way:
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding
Path=Name}"></DataGridTextColumn>
<DataGridTextColumn Header="Age" Binding="{Binding
Path=Age}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Friend">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsFriend, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Checked="CheckBox_ValueChanged"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
In my CheckBox_ValueChanged code behind, I'd like to be able to get the
Name of the person whose IsFriend CheckBox was selected. This is a rough
sketch of my code behind:
private void CheckBox_ValueChanged(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
//string name = Get the name that corresponds to the checkBox
//Perform actions using the checkbox and name
}
I'm not sure if I should be passing the row to the CheckBox_ValueChanged
method (or how to do that), or if there would be a simpler way to just get
the Name using data binding or something similar in the XAML.
No comments:
Post a Comment