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

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

问题描述

我在我的应用程序中使用TriState模式的复选框。此模式的正常行为似乎是在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.

我想更改此行为,使其循环为null,true,false 。

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

这是最好的方法是什么?

What's the best way to do this?

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

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复选框 - 如何更改状态的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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