I currently use a WPF Datagrid in a C# application, where each row contains a CheckBox, a ComboBox and a TextBox. Then, a single method is registered for CheckBox.Checked, CheckBox.Unchecked and ComboBox.SelectionChanged events. With extended selection enabled, I want to reflect the changes of a control to all selected rows (e.g. if a checkbox is unchecked, it shall be for every selected rows).
The method is called as intended when the controls are used and everything works just fine. My problem is that the event is also called when I scoll the DataGrid, which cause me some trouble. My first idea on this is that some rows are virtualized and the event is called when they are loaded, but I'm not sure how I can verify this.
What I would like is to simply ignore theses events, I use the eventArg to determine what change is done and reflect it on selected cells. But when scrolling, this cause some random behavior...
Could it be virtualized rows that call the event on loading? Could I detect this behavior and ignore these events?
Here is my DataGrid definition :
<DataGrid AutoGenerateColumns="False" Height="364" HorizontalAlignment="Left" Margin="6,40,0,0" Name="dataGrid" VerticalAlignment="Top" Width="628" CanUserResizeColumns="True" SelectionMode="Extended" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Column 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="dataGrid_checkBox" IsChecked="false" Checked="OnDataGridEvent" Unchecked="OnDataGridEvent" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Column 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="dataGrid_comboBox" SelectionChanged="OnDataGridEvent">
<ComboBoxItem Content="1" IsSelected="True" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Thanks for your help
Don't use ComboBoxItem
Bind to a List
Don't use SelectionChanged event - just use the set
struct NameBool
{
private string name;
private bool selected;
public string Name
{
get { return name; }
set
{
if (name == value) return;
name = value;
}
}
public bool Selected
{
get { return selected; }
set
{
if (selected == value) return; // this is to ignore a no change
selected = value;
}
}
public NameBool(string Name, bool Selected) { name = Name; selected = Selected;}
}