wpf datagridcheckboxcolumn:如何启用/禁用复选框

wa7juj8i  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(236)

我有一个DataGridDataGridCheckBoxColumn。网格具有对象列表的绑定。我想,如果有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");
    }
}
pb3skfrl

pb3skfrl1#

感谢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>

这里是完整的主题

kdfy810k

kdfy810k2#

是的,你可以在Triggers的帮助下做到这一点,像这样

<Style x:Key="MyCheckBoxStyle" TargetType="{x:Type CheckBox}">  
 <Style.Triggers>
  <Trigger Property="IsChecked" Value="False">
   <Setter Property="IsEnabled" Value="False" />
 </Trigger>
</Style.Triggers>

您可以将此样式应用于复选框

zlwx9yxi

zlwx9yxi3#

你可以设置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" />
                    </Style>
                </DataGridCheckBoxColumn.ElementStyle>
            </DataGridCheckBoxColumn>
        </DataGrid.Columns>
    </DataGrid>


 private bool hasMaxNumberReached;
 public bool HasMaxNumberReached
 {
  get 
     {return hasMaxNumberReached;}
  set 
     {
      hasMaxNumberReached =value; 
       RaisePropertyChanged("HasMaxNumberReached");
       }
  }

相关问题