Hi to all, I found the following little neat sample and am trying to figure out how it works. In particular, I'm having problems with understanding these few lines:
<Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"><TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"></TextBlock></Border><AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" ><Border BorderBrush="red" BorderThickness="1" /></AdornedElementPlaceholder>
In particular, why are we referring the ToolTip binding to customAdorner and not the AdornedElement (which I think would be the TextBox - see entire xaml file below)?
Also, It actually throws exceptions as soon as you type something into the text boxes, Why?
The three files are available for download at:http://cid-2c6600f1c1d5e3be.skydrive.live.com/self.aspx/.Public/WPFValidation.zip
I've attached the main xaml file below:
Thanks to all!
<Window x:Class="WPFValidation.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPFValidation" Title="Add Customer" Height="200" Width="300"><Window.Resources><!--<local:Customer x:Key="customer" />--><Style TargetType="{x:Type Label}"><Setter Property="Margin" Value="5,0,5,0" /><Setter Property="HorizontalAlignment" Value="Right" /></Style><Style TargetType="{x:Type TextBox}"><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="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"><TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"></TextBlock></Border><AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" ><Border BorderBrush="red" BorderThickness="1" /></AdornedElementPlaceholder></DockPanel></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Grid x:Name="grid" Margin="0,25,0,0"> <!--DataContext="{Binding Source={StaticResource customer}}">--><Grid.CommandBindings><CommandBinding Command="New" CanExecute="AddCustomer_CanExecute" Executed="AddCustomer_Executed" /></Grid.CommandBindings><Grid.ColumnDefinitions><ColumnDefinition Width="Auto" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="auto" /><RowDefinition Height="auto" /><RowDefinition Height="auto" /><RowDefinition Height="auto" /></Grid.RowDefinitions><Label Target="{Binding ElementName=tbFirstName}" Content="_First Name:" Grid.Column="0" Grid.Row="0" /><Label Target="{Binding ElementName=tbLastName}" Content="_Last Name:" Grid.Column="0" Grid.Row="1" /><Label Target="{Binding ElementName=tbAge}" Content="_Age:" Grid.Column="0" Grid.Row="2" /><TextBox x:Name="tbFirstName" Grid.Row="0" Grid.Column="1" Validation.Error="Validation_Error" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=True}" /><TextBox x:Name="tbLastName" Grid.Row="1" Grid.Column="1" Validation.Error="Validation_Error" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=LastName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" /><TextBox x:Name="tbAge" Grid.Row="2" Grid.Column="1" Width="50" HorizontalAlignment="left" Validation.Error="Validation_Error" MaxLength="2" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Age, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" /><Button Content="Add" Grid.ColumnSpan="2" Grid.Row="3" Grid.Column="1" Margin="0,0,10,0" HorizontalAlignment="right" VerticalAlignment="Center" Command="New" /></Grid></Window>
MarcinMR