I have a DataGrid
with DataGridCheckBoxColumn
. The grid has a binding with a list of objects. I want that, if there are N checkboxes checked, the unchecked become disabled, but I don't know how to perform the disable.
<DataGridCheckBoxColumn
x:Name="IsFixedByBracketColumn"
Header="Fissato con staffa"
Binding="{Binding isFixedByBracket, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition
Binding="{Binding
HasMaxNumberReached,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
Value="true"/>
<Condition
Binding="{Binding
IsChecked,
RelativeSource={RelativeSource Self}}"
Value="false"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
<EventSetter
Event="CheckBox.Checked"
Handler="DataGridCheckBoxColumn_Checked" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
Code for the event:
private void DataGridCheckBoxColumn_Checked(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (cb.IsChecked == true)
{
this.numberOfCheckboxesChecked++;
}
else
{
this.numberOfCheckboxesChecked--;
}
if (this.numberOfCheckboxesChecked >= maxNumOfPointsPerSide)
{
this.HasMaxNumberReached = true;
}
else
{
this.HasMaxNumberReached = false;
}
}
public bool HasMaxNumberReached
{
get {
return hasMaxNumberReached;
}
set {
hasMaxNumberReached = value;
RaisePropertyChanged("HasMaxNumberReached");
}
}
Thanks to Magnus in MSDN Forum, here is the correct answer of the problem:
<DataGrid x:Name="grid" AutoGenerateColumns="False">
<DataGrid.Resources>
<Style TargetType="CheckBox" x:Key="style">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding HasMaxNumberReached, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="true"/>
<Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="false"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
<EventSetter Event="CheckBox.Checked" Handler="DataGridCheckBoxColumn_Checked" />
<EventSetter Event="CheckBox.Unchecked" Handler="DataGridCheckBoxColumn_Checked" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn x:Name="IsFixedByBracketColumn" Header="Fissato con staffa"
Binding="{Binding isFixedByBracket, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"
ElementStyle="{StaticResource style}" EditingElementStyle="{StaticResource style}">
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
Here the complete topic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With