我有DataGrid
DataGridCheckBoxColumn
。网格具有与对象列表的绑定。我想要的是,如果选中了N个复选框,则取消选中已禁用,但我不知道如何执行禁用。
<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>
活动代码:
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");
}
}
答案 0 :(得分:3)
感谢MSDN论坛中的Magnus,以下是问题的正确答案:
<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完整主题
答案 1 :(得分:0)
您可以设置DataGridCheckBoxColumn.ElementStyle
以启用/禁用单元格,如下所述。假设HasMaxNumberReached
是ViewModel上的一个属性,它告诉我已经达到了最大复选框的计数。
<DataGrid>
<DataGrid.Columns>
<DataGridCheckBoxColumn>
<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" />
<EventSetter
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
private bool hasMaxNumberReached;
public bool HasMaxNumberReached
{
get
{return hasMaxNumberReached;}
set
{
hasMaxNumberReached =value;
RaisePropertyChanged("HasMaxNumberReached");
}
}
答案 2 :(得分:0)
是的,您可以在Triggers的帮助下完成此操作,就像这样
<Style x:Key="MyCheckBoxStyle" TargetType="{x:Type CheckBox}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
并且您可以将此样式应用于复选框