gpt4 book ai didi

c# - wpf 数据网格复选框列 : how to enable/disable checkboxes

转载 作者:太空狗 更新时间:2023-10-29 21:50:30 26 4
gpt4 key购买 nike

我有一个带有 DataGridCheckBoxColumnDataGrid。网格具有与对象列表的绑定(bind)。我想要的是,如果选中了 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");
}
}

最佳答案

感谢 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完整主题

关于c# - wpf 数据网格复选框列 : how to enable/disable checkboxes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19177525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com