Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

BindingValidationError not raising on my system in foll... RSS

3 replies

Last post Sep 01, 2009 01:35 PM by mtiede@swtechnologies.com

(0)
  • nikhilthaker

    nikhilthaker

    Member

    12 Points

    31 Posts

    BindingValidationError not raising on my system in following code ?

    Jun 03, 2009 05:10 AM | LINK

    Strange thing about this issue is it's working on one system but not on my system.

    Infact when I have copied the code from another system, it was working fine for 3-4 attemps but suddenly it had stopped working without any changes during that time. 

    May be some problem with event bubbling but i have tried raising error on validation element as well but that also not woking.

    Do we need to configure any setting to raise validations?

     

    MainPage.xaml code as follows

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="DataBindingAndValidationInSL3.MainPage"
        Width="400" Height="300" mc:Ignorable="d">
        <Grid x:Name="LayoutRoot" Background="#FF8BB4D0" BindingValidationError="LayoutRoot_BindingValidationError">
            <StackPanel Orientation="Vertical">
                <TextBlock x:Name="tbkNumber" Height="25" HorizontalAlignment="Left" Margin="50,8,0,0"
                       VerticalAlignment="Top" Width="60" Text="Number" TextWrapping="Wrap"/>
                <TextBox x:Name="txtNumber" Height="25" Margin="125,8,74,0" VerticalAlignment="Top"
                     Text="{Binding Path=Number, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true }"
                     TextWrapping="Wrap" Width="200" BindingValidationError="txtNumber_BindingValidationError"/>
                <Button x:Name="btnSubmit" Content="OK" Height="30" Width="50"></Button>
            </StackPanel>
        </Grid>

    </UserControl>

     

    MainPage.xaml.cs code as follows

     public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();

                this.BindingValidationError += new EventHandler<ValidationErrorEventArgs>(MainPage_BindingValidationError);
               
                MyData d = new MyData();
                d.Number = 4;
                this.DataContext = d;
            }

        

            void MainPage_BindingValidationError(object sender, ValidationErrorEventArgs e)
            {
                if (e.Error != null)
                {
                   //Control control =    e.OriginalSource as Control;
                   //control.BorderThickness = new Thickness(5);
                   //control.BorderBrush = new SolidColorBrush(Colors.Red);
                }
            }

         } 

    and MyData.cs code as follows

    public class MyData
        {
            private int number;
            public int Number
            {
                get
                {
                    return number;
                }
                set
                {
                    if (String.Empty.Equals(value))
                    {
                        number = 1;
                    }
                    else
                    {
                        int num = Convert.ToInt32(value);
                        if ((num < 1) || (num > 5))
                        {
                            throw new ArgumentException("Number must be between 1 and 5");
                        }
                        number = value;
                    }
                }
            }

        }

     

    Validation DataBinding Silverlight 3 Beta BindingValidationError event events BindingValidationError DataBinding and Validation event bubbling

  • Mog Liang - MSFT

    Mog Liang -...

    All-Star

    21645 Points

    2132 Posts

    Microsoft

    Re: BindingValidationError not raising on my system in following code ?

    Jun 08, 2009 04:49 AM | LINK

    TextBox will trigger Binding Validate operation when it lost focus, but in your code it works not stably.

    you could trigger textbox's validate manually. 

    XAML

                <TextBox x:Name="txtNumber" Height="25" Margin="125,8,74,0" VerticalAlignment="Top" 
                     Text="{Binding Path=Number, Mode=TwoWay, ValidatesOnExceptions=true,  UpdateSourceTrigger=Explicit}"
                     TextWrapping="Wrap" Width="200"/>
                <Button x:Name="btnSubmit" Content="OK" Height="30" Width="50" Click="btnSubmit_Click_1"></Button>
    Code  
            private void btnSubmit_Click_1(object sender, RoutedEventArgs e)
            {
                //when click button, trigger textbox's validate action
                BindingExpression be = txtNumber.GetBindingExpression(TextBox.TextProperty);
                be.UpdateSource();
            }
    some useful article and thread
    http://www.silverlightshow.net/items/Data-Validation-in-Silverlight-3.aspx
    http://silverlight.net/forums/t/83833.aspx

    Mog Liang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • dhavalmania27

    dhavalmania27

    Member

    15 Points

    25 Posts

    Re: Re: BindingValidationError not raising on my system in following code ?

    Aug 25, 2009 12:08 PM | LINK

     Hi,

    I am trying to validate the text box only when i have checkbox checked. If the checkbox is unchecked i dont want to validate the textbox and it should not give the validation error. and if its checked then it should validate it.

     

    i have written the following code..

     



     

     
    <CheckBox x:Name="AbsoluteThresholdCheckbox" Content="Absolute Threshold" Height="50" />
    <TextBox Width="100" Height="25" 
    	IsEnabled="{Binding IsChecked, ElementName=AbsoluteThresholdCheckbox}" 
            x:Name="absoluteUpperLimitText" >
    		<TextBox.Text>
                    	<Binding Path="UpperLimitAbsoluteThreshold" Mode="TwoWay" 
                            NotifyOnValidationError="{Binding IsChecked, ElementName=AbsoluteThresholdCheckbox}" 
                            ValidatesOnExceptions="True"></Binding>
    		</TextBox.Text>
    	</TextBox>
     

     
  • mtiede@swtechnologies.com

    mtiede@swtec...

    Contributor

    5384 Points

    2240 Posts

    Re: Re: BindingValidationError not raising on my system in following code ?

    Sep 01, 2009 01:35 PM | LINK

     Maybe try making the binding on the NotifyOnValidationError Mode=TwoWay?  Just a guess.