locked
Validation in Xaml. RRS feed

  • Question

  • Hi

     

         How to Validate a TextBox that acceps only numeric in Xaml.

     

    Thanks

    Senthil.

    Thursday, May 24, 2007 5:22 AM

Answers

  • Hi

     

    Please check out the code, here for the textbox i am binding to startprice of a .cs file, if we enter -ve value, then it will throw an error saying value must be +ve,

     

    i am preventing the user from entering an invalid “Start Price”. This can be accomplished by adding an ExceptionValidationRule to the Binding:

     

      <TextBox Name="StartPriceEntryForm" Grid.Row="2" Grid.Column="1"

        Style="{StaticResource textStyleTextBox}" Margin="8,5,0,5">

        <TextBox.Text>

          <Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged">

            <Binding.ValidationRules>

              <ExceptionValidationRule />

            </Binding.ValidationRules>

          </Binding>

        </TextBox.Text>

      </TextBox>

     

    and in the procedural code use the setter property, 

     

    private int startPrice;

     

    public int StartPrice

    {

    get { return this.startPrice; }

    set

    {

    if (value < 0)

    {

    throw new ArgumentException("Price must be positive");

    }

    this.startPrice = value;

    OnPropertyChanged("StartPrice");

     

    }

    }

     

    of course in order to prevent from entering non numeric text

     

    1.  Type some non-numeric characters in the “Start Price” TextBox. Notice that a red border is displayed around the TextBox. This is the simplest form of Validation working behind the scenes: the binding engine tries to convert the TextBox’s Text (of type string) to an integer so that it can set the StartPrice property, but the conversion fails. This causes the default Validation adorner to be displayed, which consists of a red rectangle around the control in error.

     

    2.  The user is informed that there is an error but no details are given about that error. Let’s solve this problem by adding a tooltip that displays an appropriate error message. Add the following Trigger to the style for TextBox 

     

      <Style x:Key="textStyleTextBox" TargetType="{x:Type TextBox}">

        <Setter Property="Foreground" Value="#333333" />

        <Setter Property="MaxLength" Value="40" />

        <Setter Property="Width" Value="392" />

        <Style.Triggers>

          <Trigger Property="Validation.HasError" Value="true">

          <Setter Property="ToolTip"

            Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>

          </Trigger>

        </Style.Triggers>

      </Style>

     

    Both Validation.HasError and Validation.Errors are attached properties, meaning that the TextBox is able to store their values although they are defined in the Validation class. The Trigger checks the value of Validation.HasError, and if it is true it sets the ToolTip of the current TextBox to its first validation error. RelativeSource={RelativeSource Self} is a special Binding Source that refers to the current element.

     

    If you go through the code under stand it .. it will solve u r problem.

     

    P.S: U need to understand that there is

     

    public event PropertyChangedEventHandler PropertyChanged;

     

    protected void OnPropertyChanged(string name)

    {

    if (PropertyChanged != null)

    {

    PropertyChanged(this, new PropertyChangedEventArgs(name));

    }

    }

    Thursday, May 24, 2007 6:47 AM

All replies

  • Hi

     

    Please check out the code, here for the textbox i am binding to startprice of a .cs file, if we enter -ve value, then it will throw an error saying value must be +ve,

     

    i am preventing the user from entering an invalid “Start Price”. This can be accomplished by adding an ExceptionValidationRule to the Binding:

     

      <TextBox Name="StartPriceEntryForm" Grid.Row="2" Grid.Column="1"

        Style="{StaticResource textStyleTextBox}" Margin="8,5,0,5">

        <TextBox.Text>

          <Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged">

            <Binding.ValidationRules>

              <ExceptionValidationRule />

            </Binding.ValidationRules>

          </Binding>

        </TextBox.Text>

      </TextBox>

     

    and in the procedural code use the setter property, 

     

    private int startPrice;

     

    public int StartPrice

    {

    get { return this.startPrice; }

    set

    {

    if (value < 0)

    {

    throw new ArgumentException("Price must be positive");

    }

    this.startPrice = value;

    OnPropertyChanged("StartPrice");

     

    }

    }

     

    of course in order to prevent from entering non numeric text

     

    1.  Type some non-numeric characters in the “Start Price” TextBox. Notice that a red border is displayed around the TextBox. This is the simplest form of Validation working behind the scenes: the binding engine tries to convert the TextBox’s Text (of type string) to an integer so that it can set the StartPrice property, but the conversion fails. This causes the default Validation adorner to be displayed, which consists of a red rectangle around the control in error.

     

    2.  The user is informed that there is an error but no details are given about that error. Let’s solve this problem by adding a tooltip that displays an appropriate error message. Add the following Trigger to the style for TextBox 

     

      <Style x:Key="textStyleTextBox" TargetType="{x:Type TextBox}">

        <Setter Property="Foreground" Value="#333333" />

        <Setter Property="MaxLength" Value="40" />

        <Setter Property="Width" Value="392" />

        <Style.Triggers>

          <Trigger Property="Validation.HasError" Value="true">

          <Setter Property="ToolTip"

            Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>

          </Trigger>

        </Style.Triggers>

      </Style>

     

    Both Validation.HasError and Validation.Errors are attached properties, meaning that the TextBox is able to store their values although they are defined in the Validation class. The Trigger checks the value of Validation.HasError, and if it is true it sets the ToolTip of the current TextBox to its first validation error. RelativeSource={RelativeSource Self} is a special Binding Source that refers to the current element.

     

    If you go through the code under stand it .. it will solve u r problem.

     

    P.S: U need to understand that there is

     

    public event PropertyChangedEventHandler PropertyChanged;

     

    protected void OnPropertyChanged(string name)

    {

    if (PropertyChanged != null)

    {

    PropertyChanged(this, new PropertyChangedEventArgs(name));

    }

    }

    Thursday, May 24, 2007 6:47 AM
  • If you set a ToolTip for StartPriceEntryForm then this does not work.

     

    The validation tooltip does not override that tooltip.

     

     - diff

    Tuesday, July 3, 2007 5:34 PM