我的应用程序中有一个使用TriState模式的CheckBox.此模式的正常行为似乎是在null,false,true之间循环.
我想改变这种行为,使其在null,true,false之间循环.
最好的方法是什么?
我尝试添加类似于此的点击处理程序:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎完全禁用了复选框.我很确定我错过了一些显而易见的东西.
Tho*_*que 24
我想事件处理程序和默认行为只是取消彼此的效果,所以复选框似乎被禁用...
其实我最近不得不做同样的事情.我必须继承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();
}
}
}
Run Code Online (Sandbox Code Playgroud)