Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the xaml code

HTML
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="203" Margin="0,5,0,0"
                 Text="{Binding UsrNm, Mode=TwoWay, ValidatesOnExceptions=True}" />


        <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="textBox2" VerticalAlignment="Top" Width="203" Margin="0,5,0,0" 
                 Text="{Binding UsrPwd, Mode=TwoWay, ValidatesOnExceptions=True}" />
        <Button Content="LogIn" Grid.Column="1" Grid.Row="2" Height="39" HorizontalAlignment="Left" Margin="112,27,0,0" Name="button1" VerticalAlignment="Top" Width="91" Click="button1_Click" />


and this is the .cs file in mailpagexaml.cs
C#
public MainPage()
        {
            InitializeComponent();
            LoginInfo lgin = new LoginInfo();
            LayoutRoot.DataContext = lgin;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ele in LayoutRoot.Children)
            {
                //In our case check for text box
                if (ele is TextBox)
                {
                    //manually update source
                    (ele as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
                }

            }
        }

//and i took a class as logininfo.cs

 public class LoginInfo
    {
        private string _usrnm;
        private string _usrpwd;

        public string UsrNm 
        {
            get { return _usrnm; }
            set { _usrnm = value; } 
        }
        public string UsrPwd
        {
            get { return _usrpwd; }
            set { _usrpwd = value; } 
        }
    }

but it is not working for validation...

i am following this tutorial
Silverlight 4 : Data Validation - Tip of the Day - Part 1[^]

please help me...
Posted
Updated 8-May-12 2:46am
Comments
Sandeep Mewara 8-May-12 8:46am    
You forgot to add, what do you mean by 'not working'?
varaprasadreddy 8-May-12 12:37pm    
validation with red color appearance indication not working....

1 solution

You're missing the actual validation call. With ValidatesOnExceptions, an exception must be thrown in the property setter, and the attributes are not taken into account automatically with respect to validation. For it to work you need a call to System.ComponentModel.DataAnnotations.Validator.ValidateProperty with the correct parameters.

However, if using Silverlight 4, I'd suggest looking into validating with IDataErrorInfo, as I feel it offers a lot more flexibility.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900