WPF
Hello,how to make validations for toolbox controls in xaml page?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
mayuriPosted Apr 24, 2013, 7:28 AM
Manoj PatelPosted Apr 24, 2013, 1:05 AM
I think there no one shot solution, u can make a public function and call every textbox
below code is vb.net
or check this way
mayuriPosted Apr 23, 2013, 6:41 AM
Manoj PatelPosted Apr 23, 2013, 12:16 AM
public class Class1 { public string Name { get; set; } }The object can be created by adding it to the window its resource collection. The source binds to the object and the path to its property [Name], which in this example is "0123456789".
-----------------------------------------
http://weblogs.asp.net/monikadyrda/archive/2009/06/24/wpf-textbox-validation.aspx
WPF TextBox Validation
Sample problem: set parameter by entering value into a TextBox, check if value is a number in given range, allow clicking a Button if validation was successful.ValidationRule
ValidationRule is an abstract class from System.Windows.Controls that helps creating custom validation rules for user input. In this example a derived class DoubleRangeRule will check if user entered a number (double) within a given range (between Max and Min value).public class DoubleRangeRule : ValidationRule
{
public double Min { get; set; }
public double Max { get; set; }
public override ValidationResult Validate( object value,
CultureInfo cultureInfo )
{
double parameter = 0;
try
{
if( ((string) value).Length > 0 )
{
parameter = Double.Parse( (String) value );
}
}
catch( Exception e )
{
return new ValidationResult( false, "Illegal characters or "
+ e.Message );
}
if( (parameter < this.Min) || (parameter > this.Max) )
{
return new ValidationResult( false,
"Please enter value in the range: "
+ this.Min + " - " + this.Max + "." );
}
return new ValidationResult( true, null );
}
}
Validation.ErrorTemplate
Having defined ValidationRule as above if user input is invalid, TextBox will have a red border. Like everything in WPF it is possible to customize it by setting a custom ControlTemplate. My template shows a warning sign and a label explaining error made by user (why input is invalid):<ControlTemplate x:Key="TextBoxErrorTemplate">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Image Height="16" Margin="0,0,5,0"
Source="Assets/warning_48.png"/>
<AdornedElementPlaceholder x:Name="Holder"/>
StackPanel>
<Label Foreground="Red" Content="{Binding ElementName=Holder,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
StackPanel>
ControlTemplate>Read more about AdornedElement here.
Binding to validation result
On the result of validation can depend many things. For example I would like to bind button IsEnabled property to that deciding to enable button only in user input is valid. Simplest way, in my opinion is to check if TextBox has any validation errors and if no – make button enabled, disabled otherwise. With TextBox called Box (x:Name) binding of Button's IsEnabled property looks like that:IsEnabled="{Binding ElementName=Box, Path=(Validation.Errors)[0],
Converter={StaticResource buttonEnabled}}"
Final app
Finally we get a TexBbox with validation rules, tips on incorrect input and a Button bound to correctness of input.
<TextBox x:Name="Box"
Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}">
<TextBox.Text>
<Binding Path="ValueInBox" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ValidationExpamples:DoubleRangeRule Min="0.5" Max="10"/>
Binding.ValidationRules>
Binding>
TextBox.Text>
TextBox>
<Button Content="SaveChanges"
IsEnabled="{Binding ElementName=Box, Path=(Validation.Errors)[0],
Converter={StaticResource buttonEnabled}}"/>
If anyone had an idea how to do things like that a better way comments would be much appreciated. VS2008 project here.
Monika
Posted: Jun 24 2009, 06:05 PM by xmonix | with 8 comment(s)
mayuriPosted Apr 22, 2013, 8:59 AM
Manoj PatelPosted Apr 22, 2013, 8:14 AM
Try this way
http://stackoverflow.com/questions/6087468/wpf-validation-of-textbox-what-should-the-binding-look-like