Tom*_*ies 4 data-binding wpf ivalueconverter
我正在尝试编写一个值转换器,以将WPF ToggleButton的Boolean IsChecked属性绑定到模型中的非布尔值(碰巧是双精度)。我编写的convert函数如下所示:
public object Convert(object value, Type targetType, object paramter, System.Globalization.CultureInfo culutre)
{
if (targetType != typeof(Boolean))
throw new InvalidOperationException("Target type should be Boolean");
var input = double.Parse(value.ToString());
return (input==0.0) ? false: true;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当调用函数时,targetType不是我期望的-它是
"System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
Run Code Online (Sandbox Code Playgroud)
而不是System.Boolean。这是预期的吗?过去我没有麻烦地写了其他转换器。
是的,因为ToggleButton(考虑复选框)可以处于三种状态:已选中,未选中和都不处于选中状态(复选框将显示为灰色)。
在MSDN库状态:
ToggleButton Class
Base class for controls that can switch states, such as CheckBox.
Run Code Online (Sandbox Code Playgroud)
Property Value
Type: System.Nullable<Boolean>
true if the ToggleButton is checked; false if the ToggleButton is unchecked; otherwise null. The default is false.
Run Code Online (Sandbox Code Playgroud)
因此,如果将其bool?强制转换为或可为空,则可以轻松地通过.HasValue和获得值.Value。
| 归档时间: |
|
| 查看次数: |
2400 次 |
| 最近记录: |