locked
How the exception is automatically handled when using ValidatesOnExceptions=True ? RRS feed

  • Question

  • Hello,

    this is the XAML markup that i am using :

    <Grid x:Name="LayoutRoot">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
    
    <TextBlock Text="Name" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">
            </TextBlock>
            
            <TextBox Grid.Row="0" Grid.Column="1" Height="25" VerticalAlignment="Top"
                     Text="{Binding Path=UserName, Mode=TwoWay,ValidatesOnExceptions=True}"
                     >
            </TextBox>
    </Grid>
    
    



    and code behind code for that.

     public class MyValidator
        {
            private string _name;
          
            public string UserName
            {
                get
                {
                    return _name;
                }
    
                set
                {
                    if (string.IsNullOrEmpty(value))
                    {
    
                        throw new Exception("please insert new value");
    
                    }
                }
            }
        }
    
    
      public partial class ValidationFormTest : Page
        {
            
    
            MyValidator validator = new MyValidator();
            public ValidationFormTest()
            {
                InitializeComponent();
               
                LayoutRoot.DataContext = validator;
            }
    
            // Executes when the user navigates to this page.
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
        }



    But i am surprised that how my thrown exception is handled so gracefully even if i am not providing any try catch block.

    I am surprised too that how validation is happening ...how TextBox border color is set to red !! how a message popup appeares when we click on right top corner of that TextBox when TextBox is left blank ?

    Wednesday, May 30, 2012 8:38 PM

Answers

All replies