6

我有一个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");
    }
}
4

3 回答 3

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> 

这里是完整的主题

于 2013-10-07T13:59:05.747 回答
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");
       }
  }
于 2013-10-04T09:36:19.363 回答
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>

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

于 2013-10-04T09:39:50.393 回答