List of validation rules in Library

Full list of rules:
  • DateTimeRangeValidationRule
  • DateTimeValidationRule
  • DateValidationRule
  • DecimalRangeValidationRule
  • NotEmptyValidationRule
  • NotNullValidationRule
  • NumericRangeValidationRule
  • PeselValidationRule
  • RequiredValidationRule
  • StringGramarValidationRule
  • StringLengthValidationRule
  • StringMaxLengthValidationRule
  • StringMinLengthValidationRule
  • StringRegexValidationRule
  • StringRegularExpressionValidationRule
  • TimeValidationRule

Validation rule usage:

1. Add validator namespace:

<Window
[...]
	xmlns:ValidationRules="clr-namespace:Alfa.Common.ValidationRules;assembly=Alfa.Common"
[...]
/>
[...]

2. Add validator:
	<TextBox>
		<Binding Path="Firstname" UpdateSourceTrigger="PropertyChanged">
			<Binding.ValidationRules>
				<ValidationRules:StringLengthValidationRule Max="255" />
			</Binding.ValidationRules>
		</Binding>
	</TextBox>

Notify example #1

You can use this simple code for notify error to the user (by tooltip)

[...]
<Window.Resources>
	<Style x:Key="FrameworkElementInError" TargetType="{x:Type FrameworkElement}">
		<Style.Triggers>
			<Trigger Property="Validation.HasError" Value="true">
				<Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
			</Trigger>
		</Style.Triggers>
	</Style>
</Window.Resources>
[...]
	<TextBox>
		<Binding Path="Firstname" UpdateSourceTrigger="PropertyChanged" Style="{DynamicResource FrameworkElementInError}">
			<Binding.ValidationRules>
				<ValidationRules:StringLengthValidationRule Max="255" />
			</Binding.ValidationRules>
		</Binding>
	</TextBox>
[...]

Notify example #2 (by second control)

Notify user by TextBlock

[...]
<Window.Resources>
[...]
	<Style x:Key="FrameworkElementInError" TargetType="{x:Type FrameworkElement}">
		<Style.Triggers>
			<Trigger Property="Validation.HasError" Value="true">
				<Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
			</Trigger>
		</Style.Triggers>
	</Style>
	<Style x:Key="errorTextBlock" TargetType="{x:Type TextBlock}">
		<Setter Property="Foreground" Value="Red" />
		<Setter Property="Margin" Value="5,0,5,5" />
		<Setter Property="TextWrapping" Value="Wrap" />
		<Setter Property="Visibility" Value="{Binding Text, Converter={StaticResource StringToVisibilityConverter}, RelativeSource={x:Static RelativeSource.Self}}" />
	</Style>
[...]
</Window.Resources>
[...]
	<TextBox x:Name="TextBox1" Style="{DynamicResource FrameworkElementInError}">
		<Binding Path="Firstname" UpdateSourceTrigger="PropertyChanged">
			<Binding.ValidationRules>
				<ValidationRules:StringLengthValidationRule Max="255" />
			</Binding.ValidationRules>
		</Binding>
	</TextBox>
[...]
<TextBlock Text="{Binding ToolTip, ElementName=TextBox1}" Style="{DynamicResource errorTextBlock}" />
[...]

Last edited Jun 24, 2013 at 6:51 AM by mklemarczyk, version 5

Comments

No comments yet.