Person.Cs
private string _Pwd;
public string Pasword
{
get
{
return _Pwd;
}
set
{
if (_Pwd != value)
{
_Pwd = value;
OnPropertyChanged("Pasword");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = "";
if (!string.IsNullOrEmpty(Pasword) && Pasword.Length < 6)
{
result="Enter maximium 6 digit character";
}
return result;
}
}
PasswordBoxAssistant.cs //this class for dependency property of Passwordbox
{
//////...//////////
}
xaml:
<Window x:Class="ITA.AdminMaster"
xmlns:local="clr-namespace:ITA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AdminMaster" Height="567" Width="917">
<Window.Resources>
<local:Person x:Key="person"/>
<local:PasswordBoxAssistant x:Key="PBA"/>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="5,0,5,0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<Style x:Key="PassVallidationTemplate" TargetType="{x:Type PasswordBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,2,40,2" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="OrangeRed" DockPanel.Dock="right" Margin="5,0,0,0"
Width="20" Height="20" CornerRadius="5"
ToolTip="{Binding ElementName=customAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center"
HorizontalAlignment="center"
FontWeight="Bold" Foreground="white"
/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<PasswordBox x:Name="Pwd" HorizontalAlignment="Left" Validation.Error="Validation_Error" Style="{StaticResource PassVallidationTemplate}"
local:PasswordBoxAssistant.BindPassword="True" local:PasswordBoxAssistant.BoundPassword="{Binding
Path=Pasword,UpdateSourceTrigger=LostFocus,ValidatesOnExceptions=True,NotifyOnValidationError=True}"
VerticalAlignment="Top" Grid.Column="3" Grid.Row="3" Width="133"
Height="21" Margin="0" Grid.RowSpan="2" /> // here i cant get style of validation error for max length
</Window>
cs
private int _errors = 0;
private Person p = new Person();
public AdminMaster()
{
InitializeComponent();
grid_AdminData.DataContext = p;
}
private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_errors++;
else
_errors--;
}