gpt4 book ai didi

wpf - TriState 复选框 - 如何更改状态的顺序

转载 作者:行者123 更新时间:2023-12-04 01:37:28 25 4
gpt4 key购买 nike

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

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

做到这一点的最佳方法是什么?

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

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;
}

}

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

最佳答案

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

实际上,我最近不得不做同样的事情。我不得不继承CheckBox并覆盖 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();
}
}
}

关于wpf - TriState 复选框 - 如何更改状态的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1343866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com