如何在WPF中设置/重置三态复选框值 [英] How to set/reset Three-state checkbox value in WPF

查看:532
本文介绍了如何在WPF中设置/重置三态复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格的标题列之一是三态复选框。该列的单元格模板包含两个状态复选框
+ AllItems复选框
- Item1
- Item2
- Item3
..
我想使用AllItems复选框以选择/取消选择工作正常的所有项目(item1,item2)。接下来,我想要将AllItems复选框设置为中间状态,而不是选中/取消选择所有项目。类似地,当所有项目被手动选择时,我想将AllItems复选框设置为选中/取消选中。

I have a datagrid whose one of the header column is Three-state checkbox. The celltemplate for that column contains two state checkbox + AllItems CheckBox - Item1 - Item2 - Item3 .. I wanted to use AllItems checkbox to select/unselect all items (item1,item2) which works fine. Next I wanted to set AllItems checkbox to intermediate state when not all items are selected/unselected. Similarly I wanted to set AllItems checkbox as checked/unchecked when all items get manually selected.

这是我尝试的代码...

Here is the code that I tried...

<dg:DataGridTemplateColumn.HeaderTemplate>
    <DataTemplate>
        <StackPanel x:Name="StackPanelForItemCheckbox" Orientation="Horizontal">
           <CheckBox x:Name="AllItemSelectionCheckBox" HorizontalAlignment="Left" Cursor="Hand"  
                     IsChecked="{Binding IsAllItemChecked, Mode=TwoWay}"                                              
                     IsThreeState="True"  Checked="ItemSelectionCheckBox_Checked" 
                     Unchecked="ItemSelectionCheckBox_Unchecked"
                     Click="AllItemSelectionCheckBox_Click">
           <TextBlock x:Name="ItemNameTextBlock" Text="Item" Margin="10,0,0,0">
           ......
<dg:DataGridTemplateColumn.CellTemplate>
       <DataTemplate x:Name="ItemCheckDataTemplate">                                
           <StackPanel x:Name="ItemCheckBoxStackPanel" Orientation="Horizontal">                                    
                  <CheckBox x:Name="itemCheckBox" Cursor="Hand" IsChecked="{Binding IsItemChecked, Mode=TwoWay}" Click="ItemSelectionCheckBox_Click"></CheckBox>
                   <TextBlock x:Name="ItemNameTextBlock" Text="{Binding Path=Item}"> </TextBlock>                                   
            </StackPanel>
         </DataTemplate>
...

ItemSelectionCheckBox_Click方法会查找所有三个状态无检查,中间),并设置IsAllItemChecked属性为INotifyproperty。这是行不通的。其他替代方法我可以尝试是找到AllItems元素并从代码中设置它。在网络上找不到类似的东西。有很少的例子,但是对于TreeView,而不是我的方式。任何帮助?

"ItemSelectionCheckBox_Click" method looks for all three state (all-checked, none-checked, intermediate) and sets "IsAllItemChecked" property which is INotifyproperty. It does not work. Other alternative I may try is to find the "AllItems" element and set it from the code. Could not locate anything like that on web. There is few examples but is for TreeView and not the way I am trying. Any help?

PS >>


  1. 我想要的第一件事是允许AllItemSelectionCheckBox手动选择时只有两个状态(True,False)。

  1. First thing I wanted was to allow "AllItemSelectionCheckBox" to have only two states (True, False) when manually selected.

private void AllItemSelectionCheckBox_Click(object sender, RoutedEventArgs e)
{
    var cb = e.Source as CheckBox;
    if (!cb.IsChecked.HasValue)
        cb.IsChecked = false;  
}


我想AllItemSelectionCheckBox复选框显示三状态直通代码。所有复选框未选中将导致其值为FALSE


    < >
  • 任何选择的几个将使其值为NULL。

  • `private void itemCheckBox_Checked(object sender, RoutedEventArgs e)
       { 
         DataGridRowsPresenter DGRPresenter = FindVisualChild(DataGName1);
          if (DGRP == null || DGRP.Children == null)
              return null;
          foreach (object obj in UIEC)
          {
             DGR = obj as Microsoft.Windows.Controls.DataGridRow;
             UIC = DGR.Item as ;
             if (DGR.IsSelected == true)
                 UIC.IsItemChecked = true;
             if (UIC.IsItemChecked == true)
                 NumberOfItemsChecked++;
          }
          if (NumberOfItemsChecked == myViewModelAllItems.Count)
          {
             allcheckbox.IsChecked = true;
          }
          else if (NumberOfItemsChecked < myViewModelAllItems.Count)
          {
            allcheckbox.IsChecked = null;   //intermittent state
          }

    更新NumberOfItemsChecked计数全局不工作,由于竞争条件损坏外部的值。


    注意:上面的代码更像是一个示例,可能无法直接复制它。

    Updating NumberOfItemsChecked count globally did not work due to race condition corrupting the value outside.

    Note: Above code is more like an example and may not work copying it directly. I can provide complete code with sample on request.

    推荐答案

    实际上我得到了一个更好的。

    Actually I got one better.

    我发现如果我为IsThreeState创建一个绑定,然后根据值是否设置更改值,然后它工作。

    I found out that if I create a binding for IsThreeState, then change the value based on whether the value is set or not, then it works.

    bool? _Value;
    public bool? Value
    {
        get { return _Value; }
        set
        {
            if (value == null)
            {
                IsThreeState = true;
            }
            else
            {
                IsThreeState = false;
            }
            _Value = value;
    
            NotifyPropertyChanged("Value");
        }
    }
    
    bool _IsThreeState = true;
    public bool IsThreeState
    {
        get { return _IsThreeState; }
        private set
        {
            _IsThreeState = value;
            NotifyPropertyChanged("IsThreeState");
        }
    }
    


    $ b $ p

    现在复选框将支持threestate IFF外部null。
    如果值为true或false,用户将无法将其设置为null。

    Now the checkbox will support threestate IFF the value is set to null externally. If the value is true or false, the user won't be able to set it to null.

    这篇关于如何在WPF中设置/重置三态复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆