来源->目标绑定验证 (WPF)

发布于 2024-12-12 02:58:16 字数 1073 浏览 0 评论 0原文

如果我有一个绑定到数据源的 WPF 控件,并将 Binding 对象上的 ValidatesOnExceptions 设置为 true。现在,如果我在控件中写入无法转换为源数据类型的内容,我将在控件上收到验证错误和红色边框。

但是,如果我在源上设置不兼容的值,则会在绑定中静默捕获异常并返回默认值。没有迹象表明由于绑定错误,控件中的值不可信。

如何向用户可视化绑定问题,无论问题是哪一方造成的?我想让用户知道控件中的值不可靠。

更多信息:

这是我在跟踪中得到的内容。它告诉我在转换过程中抛出了 OverflowException(因为 99999 不适合 Int16)。然后在目标上设置值时,它只是使用 0 而不是 99999。没关系,我不指望它能完成不可能的事。问题是它没有给我一个对错误采取行动的选项并且没有发现验证错误。

从 DataClass 获取 PropertyChanged 事件(哈希=2616333)
使用 RuntimePropertyInfo(Int32) 从 DataClass (hash=2616333) 获取级别 0 的值:'99999'
TransferValue - 获得原始值“99999”
mscorlib.dll 中发生了“System.OverflowException”类型的第一次机会异常 System.Windows.Data 错误:6:“SystemConvertConverter”转换器无法转换值“99999”(类型“Int32”);如果可用,将使用后备值。绑定表达式:Path=Int32; DataItem='DataClass' (HashCode=2616333);目标元素是“AliasClass”(HashCode=32866918);目标属性是“Int16”(类型“Int16”)OverflowException:“System.OverflowException:值对于 Int16 来说太大或太小。
TransferValue - 生成的隐式转换器 {DependencyProperty.UnsetValue}
TransferValue - 使用后备/默认值“0”
TransferValue - 使用最终值“0”

If I have a WPF control bound to a data source and set the ValidatesOnExceptions to true on the Binding object. Now if I write something in the control that cannot be converted to the source's data type, I will get a validation error and a red border on my control.

But if I set an incompatible value on the source, the exception is silently caught in the binding and a default value is returned. No indication exists that the value in the control cannot be trusted, because of the binding error.

How can I visualize the binding problems to the user, regardless of which side is the cause of it? I want the user to know that the value in the control is not reliable.

MORE INFO:

This is what I have in the trace. It tells me that an OverflowException is thrown during conversion (because 99999 does not fit into an Int16). Then it simply uses 0 instead of 99999 when setting the value on the target. That's ok, I don't expect it to do the impossible. The problem is that it does not give me an option to act on the error and there are no validation errors to be found.

Got PropertyChanged event from DataClass (hash=2616333)
GetValue at level 0 from DataClass (hash=2616333) using RuntimePropertyInfo(Int32): '99999'
TransferValue - got raw value '99999'
A first chance exception of type 'System.OverflowException' occurred in mscorlib.dll
System.Windows.Data Error: 6 : 'SystemConvertConverter' converter failed to convert value '99999' (type 'Int32'); fallback value will be used, if available. BindingExpression:Path=Int32; DataItem='DataClass' (HashCode=2616333); target element is 'AliasClass' (HashCode=32866918); target property is 'Int16' (type 'Int16') OverflowException:'System.OverflowException: Value was either too large or too small for an Int16.
TransferValue - implicit converter produced {DependencyProperty.UnsetValue}
TransferValue - using fallback/default value '0'
TransferValue - using final value '0'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-12-19 02:58:16

为了可视化绑定问题,我知道有两种方法:

1)创建一个仅返回值的 IValueConverter,并检查该值。
这对于确保传递的变量是您想要的变量特别有用。
它是这样的:

 public class BindingTestConverter : IValueConverter, IMultiValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

2)利用PresentationTraceSource:

<UserControl (...) PresentationTraceSources.TraceLevel="High" (...) />

这将在Visual Studio的“输出窗口”中向您显示视图中的绑定错误。

To visualize Binding problems, there are 2 ways i know:

1) Make an IValueConverter that just returns the value, and inspect that value.
This is particularly useful to make sure the variable passed around is the one you want.
It goes like this:

 public class BindingTestConverter : IValueConverter, IMultiValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

2) Make use of PresentationTraceSource:

<UserControl (...) PresentationTraceSources.TraceLevel="High" (...) />

This will show you in the "Output Window" of visual studio the binding errors in your views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文