locked
range validation in wpf textbox RRS feed

  • Question

  • how to set range in validation in textbox?
    Friday, April 10, 2009 3:09 AM

Answers

  • You could use ValidationRule to check whether the data is right.

    For example:

    XAML
    <Window x:Class="SimpleBindValidation.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:SimpleBindValidation"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:MyDataSource x:Key="ods" x:Name="MyDataSource1"/>
        </Window.Resources>
        <StackPanel>
            <TextBox Height="23"   Name="textBox1" VerticalAlignment="Top"   >
                <TextBox.Text>
                    <Binding  NotifyOnValidationError="True" 
                              UpdateSourceTrigger="PropertyChanged" Path="Username" Source="{StaticResource ods}"     >
                        <Binding.ValidationRules>
                            <local:StringValidationError/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <Button HorizontalAlignment="Left"   Name="button1" Width="75" Height="23" VerticalAlignment="Top" Content="Submit" Click="button1_Click">
            </Button>
        </StackPanel>
    </Window>
    

    C#
    using System.Windows;
    using System.Windows.Controls;
    using System.Globalization;
    namespace SimpleBindValidation
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, RoutedEventArgs e)
            { 
                bool HasError = Validation.GetHasError(textBox1);
                if (HasError)
                {
                    MessageBox.Show("Error");
                }
                else
                {
                    //...
                }
            }
        }
        public class StringValidationError : ValidationRule
        { 
            public StringValidationError()
            {
            }
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
                string str = value.ToString();
                if ( str.Length > 5)
                {
                    return new ValidationResult(false, "error info");
                }
                else
                {
                    return ValidationResult.ValidResult;
                }  
            }
        }
        public class MyDataSource
        {
            private string _Username;
            public MyDataSource()
            {
                Username = "tom"; 
            }
            public string Username
            {
                get { return _Username; }
                set { _Username = value; }
            }
        }
    }
    
    • Proposed as answer by Tao Liang Tuesday, April 14, 2009 2:57 AM
    • Marked as answer by Tao Liang Tuesday, April 14, 2009 8:24 AM
    Tuesday, April 14, 2009 2:57 AM

All replies

  • Hi,

    As in limit the length of text?
    Friday, April 10, 2009 3:10 AM
  • You could use ValidationRule to check whether the data is right.

    For example:

    XAML
    <Window x:Class="SimpleBindValidation.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:SimpleBindValidation"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:MyDataSource x:Key="ods" x:Name="MyDataSource1"/>
        </Window.Resources>
        <StackPanel>
            <TextBox Height="23"   Name="textBox1" VerticalAlignment="Top"   >
                <TextBox.Text>
                    <Binding  NotifyOnValidationError="True" 
                              UpdateSourceTrigger="PropertyChanged" Path="Username" Source="{StaticResource ods}"     >
                        <Binding.ValidationRules>
                            <local:StringValidationError/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <Button HorizontalAlignment="Left"   Name="button1" Width="75" Height="23" VerticalAlignment="Top" Content="Submit" Click="button1_Click">
            </Button>
        </StackPanel>
    </Window>
    

    C#
    using System.Windows;
    using System.Windows.Controls;
    using System.Globalization;
    namespace SimpleBindValidation
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, RoutedEventArgs e)
            { 
                bool HasError = Validation.GetHasError(textBox1);
                if (HasError)
                {
                    MessageBox.Show("Error");
                }
                else
                {
                    //...
                }
            }
        }
        public class StringValidationError : ValidationRule
        { 
            public StringValidationError()
            {
            }
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
                string str = value.ToString();
                if ( str.Length > 5)
                {
                    return new ValidationResult(false, "error info");
                }
                else
                {
                    return ValidationResult.ValidResult;
                }  
            }
        }
        public class MyDataSource
        {
            private string _Username;
            public MyDataSource()
            {
                Username = "tom"; 
            }
            public string Username
            {
                get { return _Username; }
                set { _Username = value; }
            }
        }
    }
    
    • Proposed as answer by Tao Liang Tuesday, April 14, 2009 2:57 AM
    • Marked as answer by Tao Liang Tuesday, April 14, 2009 8:24 AM
    Tuesday, April 14, 2009 2:57 AM