Saturday, 1 March 2014

How to display the Validation Error message of the Property inside a binding control - WPF(XAML)

In this article we are going to see how to bind the error message of validation  of a Property to display inside the binded control, For that we have to place a textbox inside a adorned element placeholder and bind the textbox of it to the element name of adorned and path to the AdornedElement.(Valiadtion.Errors)[0].ErrorContent, Here Validation.Errors is array which contains many errors from that we are taking the first error message from the property called ErrorContent .

 <AdornedElementPlaceholder x:Name="adorelement">
                        <TextBlock Text="{Binding ElementName=adorelement,
                          Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"  
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center" Foreground="OrangeRed" FontSize="12">                         </TextBlock>
 </AdornedElementPlaceholder>


Xaml:

<Window x:Class="AdornerSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="errorTemplate">
            <DockPanel LastChildFill="True">
                <Border BorderBrush="OrangeRed" BorderThickness="1">
                    <AdornedElementPlaceholder x:Name="adorelement">
                        <TextBlock Text="{Binding ElementName=adorelement,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"  VerticalAlignment="Center"
                                   HorizontalAlignment="Center" Foreground="OrangeRed" FontSize="12"></TextBlock>
                    </AdornedElementPlaceholder>
                </Border>
            </DockPanel>   
        </ControlTemplate>
       
        <Style x:Key="txt" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Right"></Setter>
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="14" />           
            <Setter Property="FontFamily" Value="Arial" />                
        </Style>
        <Style TargetType="TextBox" x:Key="mantxt">
            <Setter Property="Margin" Value="5"    />
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Height" Value="50" />
            <Setter Property="Width" Value="250"/>
        </Style>
       
        <Style TargetType="TextBox" x:Key="katxt" BasedOn="{StaticResource mantxt}">
            <Setter Property="Foreground" Value="Green" />
        </Style>
       
        <Style x:Key="acc" TargetType="AccessText">
            <Setter Property="HorizontalAlignment" Value="Right"></Setter>
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="FontFamily" Value="Arial" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="2*" />          
        </Grid.ColumnDefinitions>
        <AccessText Grid.Row="0" Style="{StaticResource acc}" >Name</AccessText>
        <TextBox Style="{StaticResource mantxt}" Grid.Column="1" Validation.ErrorTemplate="{StaticResource errorTemplate}">
            <TextBox.Text>
                <Binding Path="Name">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <TextBlock Style="{StaticResource txt}" Grid.Row="1">Designation</TextBlock>
        <TextBox Text="{Binding Designation}"  Style="{StaticResource katxt}" Grid.Row="1" Grid.Column="1"></TextBox>
    </Grid>
</Window>



C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AdornerSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Employee emp = new Employee();

        public MainWindow()
        {
       
            InitializeComponent();
            emp.Designation = "Programmer";
            DataContext = emp;
        }
    }
}




Output:



From this article we can understand how the control bind the error message from the property.

No comments:

Post a Comment