A.R*_*.R. 43 c# wpf ivalueconverter valueconverter
在过去一年左右的时间里,我看到许多不同的价值转换器出于许多不同的目的,来自许多不同的作者.在我的脑海中突出的一件事是它们返回的"默认"值的广泛变化.例如;
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// OK, we test for some undesirable, unconvertable situation, typically null...
if (value == null)
{
// And here are a variety of 'defaults' that I have seen, these begin the most typical.
return null;
return DependencyProperty.UnsetValue;
return Binding.DoNothing;
}
//...... other code.. whatever...
}}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,是否有一种"标准"方式来表明输入值无法转换?
m-y*_*m-y 28
数据绑定引擎不会捕获用户提供的转换器抛出的异常.Convert方法抛出的任何异常,或Convert方法调用的方法抛出的任何未捕获的异常都被视为运行时错误.通过返回DependencyProperty.UnsetValue处理预期的问题.
关键是通过返回DependencyProperty.UnsetValue来处理预期的问题.
Ern*_*rno 23
当您查看这些值时,您将发现它们的含义.然后选择正确的一个返回转换器.
主要问题是null可能是属性的有效值.
DependencyProperty.UnsetValue指示属性存在,但没有由属性系统设置其值
Binding.DoNothing指示绑定引擎不将值传递给绑定目标,不移动到PriorityBinding中的下一个绑定,或者不使用FallBackValue或默认值
编辑
要表明您无法转换该值,您应该只返回给定的值.这是您可以做的最好的,因为它将问题返回给绑定的作者.如果你干涉价值,就很难找到正在发生的事情.
| 归档时间: |
|
| 查看次数: |
14445 次 |
| 最近记录: |