none
Binding.ValidatesOnExceptions=True does not include conversion exceptions ?! RRS feed

  • Question

  • Is it correct that Binding.ValidatesOnExceptions does not include exceptions occurring during back conversions?

    In my opinion, this behaviour is not only not documented and therefore misleading, it would also be very handy (consider TextBoxes used for Date/Number input.)

    Examle: The following program crashed when typing into the TextBox

        <Window.Resources> 
            <local:ConvertBackFailsConverter x:Key="CbfConverter"/> 
        </Window.Resources> 
         
        <DockPanel Margin="10"
            <TextBlock DockPanel.Dock="Top" Text="{Binding ElementName=_main,Path=Tag.Length}"/> 
            <TextBox Text="{Binding ElementName=_main,Path=Tag,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True,Converter={StaticResource CbfConverter}}"/> 
        </DockPanel> 


        public class ConvertBackFailsConverter : IValueConverter 
        { 
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
            { 
                return value; 
            } 
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
            { 
                throw new Exception("back conversion always fails (by intention)"); 
            } 
        } 
     
        public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
                Tag = "initial value"
            } 
        } 
     


    Friday, October 10, 2008 8:13 AM

Answers

  • -> In my opinion, this behaviour is not only not documented and therefore misleading, it would also be very handy
    Binding.ValidatesOnExceptions only works if you don't explicitly specify an IValueConverter and use the default value converter to convert the value back to source, the following sample works:

    <DockPanel Margin="10">
      <TextBlock DockPanel.Dock="Top" x:Name="_main"/>
      <TextBox>
        <TextBox.Text>
          <Binding
            Path="Width"
            ElementName ="_main"
            UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
              <ExceptionValidationRule/>
            </Binding.ValidationRules>
          </Binding>
        </TextBox.Text>
      </TextBox>
    </DockPanel>

    Hope this clears things up a little bit.

    • Marked as answer by Marco Zhou Friday, October 17, 2008 10:24 AM
    Tuesday, October 14, 2008 7:23 AM

All replies

  • -> In my opinion, this behaviour is not only not documented and therefore misleading, it would also be very handy
    Binding.ValidatesOnExceptions only works if you don't explicitly specify an IValueConverter and use the default value converter to convert the value back to source, the following sample works:

    <DockPanel Margin="10">
      <TextBlock DockPanel.Dock="Top" x:Name="_main"/>
      <TextBox>
        <TextBox.Text>
          <Binding
            Path="Width"
            ElementName ="_main"
            UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
              <ExceptionValidationRule/>
            </Binding.ValidationRules>
          </Binding>
        </TextBox.Text>
      </TextBox>
    </DockPanel>

    Hope this clears things up a little bit.

    • Marked as answer by Marco Zhou Friday, October 17, 2008 10:24 AM
    Tuesday, October 14, 2008 7:23 AM
  • > Hope this clears things up a little bit.

    Thanks for the reply, but I'm not yet sure whether my problem is solved:

    1) I don't understand why you explicitly use the <ExceptionValidationRule/>. In your example, setting Binding.ValidatesOnExceptions=true would have had the same effect.

    2) The point of my initial post was to have a converter
    without the need of writing an additional validation rule, since you could take a conversion exception as a signal that the input was incorrect. I understand that this is not really a clean exception handling according to coding standards (catch only those exception that you know the circumstances of the throwing), but nevertheless it would reduce the amount of source code. Obviously, the WPF creators did not have the intention to support this scenario.

    Kuno
    Thursday, October 16, 2008 7:28 AM
  • -> I don't understand why you explicitly use the <ExceptionValidationRule/>. In your example, setting Binding.ValidatesOnExceptions=true would have had the same effect.

    The snippet I post above only uses to demonstrate that ValidatesOnExceptions (aka via ExceptionValidationRule) only works for default, non-explicit converter, try keeping the point sticky.

    -> Obviously, the WPF creators did not have the intention to support this scenario.

    Yes, that's why you need to change your design decision.

    Thanks
    Thursday, October 16, 2008 9:04 AM