【问题标题】:Forcing a tri-state checkbox to not move to indeterminate state强制三态复选框不移动到不确定状态
【发布时间】:2011-04-24 15:52:05
【问题描述】:

我在 WPF 中工作,我有一个有趣的需求。 我需要我的复选框是 ThreeState,所以如果只选择了一些子元素,它会显示为不确定。但是当用户点击它时,我希望它选择真或假。

这是一个代表我的要求的故事:

item - indeterminate  
    subItem - checked  
    subItem - unchecked  
    subItem - checked  

当用户点击item 时,复选框应在选中和未选中之间交替。用户永远不能选择“不确定”作为状态。这可能吗?

【问题讨论】:

    标签: wpf checkbox


    【解决方案1】:

    XAML:

    <CheckBox IsThreeState="True" IsChecked="{x:Null}" Click="CheckBox_Clicked" />
    

    代码隐藏:

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

    如果您不喜欢代码隐藏解决方案,那么您可以像在this 问题的解决方案中那样对您自己的控件进行子类化。

    【讨论】:

    • 宾果游戏!谢了哥们。用 Indeterminate 事件尝试过,它没有用,但点击确实......奇怪:)
    • 复选框也可以使用空格键切换,因此您使用点击处理程序的解决方案是不够的。
    【解决方案2】:

    如果您将 Bi​​nding 与 CheckBox 一起使用,会容易得多。

    XAML:

    <Window x:Class="WpfApplication39Checkbox.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="128,95,0,0" VerticalAlignment="Top" IsThreeState="False" IsChecked="{Binding CheckState}"/>
            <Button Content="Button" HorizontalAlignment="Left" Margin="46,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
            <Button Content="Button" HorizontalAlignment="Left" Margin="139,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
            <Button Content="Button" HorizontalAlignment="Left" Margin="235,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
        </Grid>
    </Window>
    

    代码隐藏:

    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows;
    
    namespace WpfApplication39Checkbox
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            private bool? checkState;
    
            public bool? CheckState
            {
                get { return checkState; }
                set
                {
                    checkState = value;
                    OnPropertyChanged("CheckState");
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                CheckState = false;
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                CheckState = true;
            }
    
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                CheckState = null;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    你明白了吗?您将 CheckBox 设置为 IsThreeState="False" 但从 CodeBehind 设置了第三个状态,并且 CheckBox 的行为与预期一样。

    【讨论】:

    • 这应该是公认的答案。
    猜你喜欢
    • 2014-06-06
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2022-08-23
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多