WPF 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...
}}
所以我的问题是,是否有一种“标准”方式来指示输入值无法转换?
Over the course of the last year or so I have seen many different value converters for many different purposes, from many different authors. One thing that sticks out in my mind is the wide variance of the 'default' values that are returned by them. For example;
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...
}}
So my question is, is there a 'standard' way to indicate that an input value can't be converted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 MSDN - IValueConverter:
关键行是通过返回 DependencyProperty.UnsetValue 处理预期问题。
According to MSDN - IValueConverter:
The key line is Handle anticipated problems by returning DependencyProperty.UnsetValue.
当您查找这些值时,您就会明白它们的含义。然后选择正确的一个返回到转换器中。
主要问题是 null 可能是该属性的有效值。
DependencyProperty.UnsetValue 指示该属性存在,但没有由属性系统设置其值
Binding.DoNothing 指示绑定引擎不要将值传输到绑定目标,不要移动到 PriorityBinding 中的下一个 Binding,或者不要使用 FallBackValue 或默认值
编辑
要表明您无法转换该值,您应该简单地返回给定值。这是您能做的最好的事情,因为它将问题返回给绑定的作者。如果你干预这个值,就很难找出发生了什么。
When you look these values up you will find out what they mean. Then pick the right one to return in your converter.
The main issue is that null might be a valid value for the property.
DependencyProperty.UnsetValue to indicate that the property exists, but does not have its value set by the property system
Binding.DoNothing to instruct the binding engine not to transfer a value to the binding target, not to move to the next Binding in a PriorityBinding, or not to use the FallBackValue or default value
EDIT
To indicate that you can't convert the value you should simply return the given value. That is the best you can do because it returns the problem to the author of the binding. If you meddle with the value it becomes very hard to findout what is going on.
经过多次思考和挖掘,似乎 DependencyProperty.UnsetValue 是合适的选择。我已经将内部的所有内容都转移到了这种模式,并取得了很大的成功。此外,此页面的“备注”部分中的文本表示我认为这可能是最好的选择..
还有一些关于如果无法转换绑定则返回输入值的讨论,但这很容易“破坏”绑定系统。考虑一下绑定输入是“字符串”而输出是“画笔”的情况。返回字符串是行不通的!
After much thinking and digging around, it seems that DependencyProperty.UnsetValue is the appropriate choice. I have moved everything in house over to this pattern with much success. Also, the text in the 'Remarks' section of this page indicates that I this is probably the best choice..
There is also some discussion about returning the input value if a binding can't be converted, but this can easily "break" the binding system. Think of a case where the binding input is a 'string' and the output is a 'brush'. Returning a string is not going to work!
默认返回的内容取决于具体情况。您不想返回 int 作为 bool 转换器的默认值,或者为可见性枚举转换器返回 bool 值。
what you return as a default depends on the situation. You don't want to return an int as the default for a converter to a bool, or return a bool for a converter for the visibility enum.
通常,如果无法转换某个值,我会抛出
Exception
这是因为,如果我尝试使用无效转换器转换某个值,我希望收到警报,以便我可以改变我的代码。
在某些罕见的情况下,即使无法转换,值也可能有效,在这种情况下,我通常返回传递给转换器的相同值。仅当我希望在可能的情况下转换值,或者如果不希望值保持不变时,才使用此方法。
我偶尔遇到的另一个罕见情况是硬编码默认值。当我知道转换器可能与无效参数一起使用,并且无论结果是什么我都想返回有效值时,通常会执行此操作。我的硬编码默认转换器几乎总是返回布尔值。
我认为我从未返回过您指定的 3 个中的任何一个(
null
、DependencyProperty.UnsetValue
或Binding.DoNothing
),因为这些除非您专门寻找它们,否则值通常是意想不到的并且不容易注意到。Usually if a value can't be converted, I throw an
Exception
This is because if I'm trying to convert a value using an invalid converter, I'd like to be alerted of it so I can alter my code.
In some rare cases, a value may be valid even though it can't be converted, and in that case I usually return the same value that was passed to the converter. This is only used if I want the value to be converted if possible, or to stay the same if not.
Another rare case that I've done on occasion is hardcoding a default value. This is usually done when I know the Converter may be used with an invalid parameter, and I want to return a valid value no matter what the result is. My hard-coded default converters almost always return boolean values.
I don't think I have ever returned any of the 3 you specified (
null
,DependencyProperty.UnsetValue
, orBinding.DoNothing
) because those values are often unexpected and not easy to notice unless you specifically look for them.您还可以使用此函数返回真实的
targetType
默认对象:来源:C# - 默认(类型)的编程等效项
You could also return a real
targetType
default object by using this function:Source: C# - Programmatic equivalent of default(Type)