我的应用程序中有一个使用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;
}
}
但这似乎完全禁用了复选框。我很确定我错过了一些显而易见的东西。
答案 0 :(得分: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();
}
}
}
答案 1 :(得分:0)
仅出于完整性考虑,此处还提供Windows窗体解决方案。我们必须重写 $('.woocommerce-pagination').replaceWith($(data).find('#hmuPagination').show());
$('.woocommerce-result-count').replaceWith($(data).find('#hmuCount').show());
方法,重写OnClick
会导致显示错误(如果执行不正确,则会导致堆栈溢出)。
OnStateChanged
答案 2 :(得分:0)
非常感谢,使用ToggleButton解决类似行为非常有用。
class InvertedToggleButton : ToggleButton
{
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(ToggleButtonInvertido), 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
{
if (!this.IsChecked.HasValue)
this.IsChecked = true;
else
base.OnToggle();
}
}
}
答案 3 :(得分:0)
如前所述,在WinForms中没有要覆盖的{
"@t": "2018-12-03",
"@mt": "{@Diagnostic}",
"Diagnostic": {
"$type": "IqhApplicationEndEvent" ---------Sorted to the top
"ShutdownReason": "HostingEnvironment",
"Timestamp": "2018-12-03",
"MachineName": "local",
"AdditionalInfo": {},
}
}
,因此在这种情况下CREATE TABLE [dbo].[company](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) NOT NULL,
[shortform] [varchar](20) NOT NULL)
是要去的地方。
这是WinForms复选框的最小实现,该复选框循环未选中->不确定->已选中->未选中...,而不是默认的未选中->已选中->不确定->未选中...
ALTER FUNCTION [dbo].[StringPrio] (
@prioStr varchar(max),
@fallbackStr varchar(max))
RETURNS varchar(max)
AS BEGIN
DECLARE @returnStr varchar(max) = ''
IF @prioStr <> '' BEGIN
SET @returnStr = @prioStr
END ELSE BEGIN
SET @returnStr = @fallbackStr
END
RETURN @returnStr
END
或者,如果您很乐意引入对Windows分配给CheckState哪些状态的int值的依赖性,则这也可以工作:
SELECT dbo.StringPrio(shortform,name) AS nameShort FROM company
答案 4 :(得分:0)
可接受的解决方案是最好的,但是如果您不想创建子类,只需执行以下操作:
void chkbokHeader_Click(object sender, RoutedEventArgs e)
{
CheckBox senderChk = sender as CheckBox;
bool bCheck = false;
//here, the status is already changed, so rechange:
if (senderChk.IsChecked == true) // from UNCHECKED
bCheck = true;
else if (senderChk.IsChecked == false) //from INDETERMINATE
bCheck = true;
else //from CHECKED
bCheck = false;
senderChk.IsChecked = bCheck;
e.Handled = true;
}
它创建(检查)序列:不确定-> true-> false-> true-> false-> true-> ...