gpt4 book ai didi

c# - WPF 值转换器 - 不可转换值的标准返回

转载 作者:IT王子 更新时间:2023-10-29 03:55:38 26 4
gpt4 key购买 nike

在过去一年左右的时间里,我从许多不同的作者那里看到了许多用于许多不同目的的不同值转换器。在我脑海中突出的一件事是它们返回的“默认”值的广泛差异。例如;

  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...
}}

所以我的问题是,是否有一种“标准”方式来指示无法转换输入值?

最佳答案

根据 MSDN - IValueConverter :

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyProperty.UnsetValue.

关键行是通过返回 DependencyProperty.UnsetValue 来处理预期的问题。

关于c# - WPF 值转换器 - 不可转换值的标准返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5992769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com