Validation rule not working...
Hi there,
i have been trying to implement a custom validation rule. It doesn't seem to be working though I am not getting any errors. I figured out that I needed this line.
Code:
xmlns:local="clr-namespace:validatiinTest"
Yes - I know I spelled Validation wrong when I created the project.
Everything compiles but nothing happens when I change the value in the textbox that has the validation rule... I mean in the code, I dropped a breakpoint inside the validate method and it never gets called - what am I missing? Below is the code for the whole thing... As far as the book that I have is concerned that is everything I should need. Of course I am not doing anything or checking it but still Validate() should get called.
Code:
<Window x:Class="validatiinTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:validatiinTest"
Title="Window1" Height="300" Width="300" Background="Aqua">
<Grid>
<TextBox Height="21" Margin="12,62,0,0" Name="TextBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" >
<TextBox.Text>
<Binding Path="xxxx" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:CheckForInteger />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Height="23" Margin="77,0,81,29" Name="textBox2" VerticalAlignment="Bottom" />
</Grid>
</Window>
Code:
namespace validatiinTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class CheckForInteger : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureinfo)
{
int theNum = 0;
if (!int.TryParse(value.ToString(), out theNum))
return new ValidationResult(false, "Not a number");
return new ValidationResult(true, "No Error");
}
}
}
Thanks!
Re: Validation rule not working...
Have a look at Binding Validation Sample from MSDN if it helps.