0
votes

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'

1
According to the book WPF 4 Unleashed, validation only works when pushing data from the target to the source and not the other way around.Kristoffer

1 Answers

1
votes

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.