TriState Checkbox - 如何更改状态的顺序 [英] TriState Checkbox - how to change the order of the states

查看:12
本文介绍了TriState Checkbox - 如何更改状态的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个使用 TriState 模式的 CheckBox.这种模式的正常行为似乎是在 null、false、true 之间循环.

I have a CheckBox in my application that is using the TriState mode. The normal behavior for this mode seems to be cycling between null, false, true.

我想改变这种行为,让它在空、真、假之间循环.

I'd like to change this behavior so that it cycles between null, true, false.

最好的方法是什么?

我已经尝试添加一个类似于这样的点击处理程序:

I've tried adding a click handler similar to this:

void cb_Click(object sender, RoutedEventArgs e)
{
    if (((CheckBox)e.Source).IsChecked.HasValue == false)
    {
        ((CheckBox)e.Source).IsChecked = true;
        return;
    }

    if (((CheckBox)e.Source).IsChecked == true)
    {
        ((CheckBox)e.Source).IsChecked = false;
        return;
    }

    if (((CheckBox)e.Source).IsChecked == false)
    {
        ((CheckBox)e.Source).IsChecked = null;
        return;
    }

}

但这似乎完全禁用了复选框.我很确定我遗漏了一些应该很明显的东西.

But this seems to disable the checkbox entirely. I'm pretty sure I'm missing something that should be obvious.

推荐答案

我猜事件处理程序和默认行为只是取消了彼此的效果,所以复选框似乎被禁用了...

I guess the event handler and the default behavior are just cancelling each other's effects, so the checkbox seems disabled...

实际上我最近不得不做同样的事情.我必须从 CheckBox 继承并覆盖 OnToggle :

Actually I recently had to do the same thing. I had to inherit from CheckBox and override OnToggle :

public class MyCheckBox : CheckBox
{


    public bool InvertCheckStateOrder
    {
        get { return (bool)GetValue(InvertCheckStateOrderProperty); }
        set { SetValue(InvertCheckStateOrderProperty, value); }
    }

    // Using a DependencyProperty as the backing store for InvertCheckStateOrder.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty InvertCheckStateOrderProperty =
        DependencyProperty.Register("InvertCheckStateOrder", typeof(bool), typeof(MyCheckBox), new UIPropertyMetadata(false));

    protected override void OnToggle()
    {
        if (this.InvertCheckStateOrder)
        {
            if (this.IsChecked == true)
            {
                this.IsChecked = false;
            }
            else if (this.IsChecked == false)
            {
                this.IsChecked = this.IsThreeState ? null : (bool?)true;
            }
            else
            {
                this.IsChecked = true;
            }
        }
        else
        {
            base.OnToggle();
        }
    }
}

这篇关于TriState Checkbox - 如何更改状态的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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