WPF 验证错误模板
<Window x:Class="BindingExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingExam"
Title="MainWindow" Height="413.027" Width="488.793" FontSize="16">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14" FontWeight="Bold"
ToolTip="{Binding ElementName=PlaceHolder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">*</TextBlock>
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder Name="PlaceHolder"></AdornedElementPlaceholder>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Margin="3" Name="lstProducts" DisplayMemberPath="ModelName" VirtualizingPanel.CacheLength="3"></ListBox>
<StackPanel Margin="3" Grid.Row="1" Orientation="Horizontal">
<Button Margin="3" Grid.Row="1" Name="btnGetProducts" Click="btnGetProducts_Click_1">GetProducts</Button>
<Button Margin="3" Grid.Row="1" Name="btnDelete" Click="btnDelete_Click_1">Delete</Button>
<Label VerticalAlignment="Center">UnitCost></Label>
<TextBox Width="200" Name="txtMinUnitCost">0</TextBox>
</StackPanel>
<Grid Grid.Row="2" Name="gridProduct" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Margin="3">Model Number:</Label>
<TextBox Name="txtModelNumber" Margin="3" Grid.Column="1" Text="{Binding Path=ModelNumber, Mode=OneWay}" Grid.ColumnSpan="2"></TextBox>
<Label Margin="3" Grid.Row="1">Model Name:</Label>
<TextBox Name="txtModelName" Margin="3" Grid.Column="1" Grid.Row="1" Text="{Binding Path=ModelName, Mode=OneWay}" Grid.ColumnSpan="2"></TextBox>
<Label Margin="3" Grid.Row="2">Unit Cost:</Label>
<TextBox Name="txtUnitCost" Margin="3" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Validation.Error="txtUnitCost_Error_1">
<TextBox.Text>
<Binding Path="UnitCost" NotifyOnValidationError="True">
<Binding.ValidationRules>
<!--<ExceptionValidationRule></ExceptionValidationRule>-->
<!--<DataErrorValidationRule></DataErrorValidationRule>-->
<local:PositivePriceRule Min="0" Max="1000"></local:PositivePriceRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Margin="3" Grid.Row="3">Description:</Label>
<TextBox Name="txtDescription" Margin="3" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3" Text="{Binding Path=Description, Mode=OneWay}"
TextWrapping="Wrap"></TextBox>
</Grid>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingExam"
Title="MainWindow" Height="413.027" Width="488.793" FontSize="16">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14" FontWeight="Bold"
ToolTip="{Binding ElementName=PlaceHolder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">*</TextBlock>
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder Name="PlaceHolder"></AdornedElementPlaceholder>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Margin="3" Name="lstProducts" DisplayMemberPath="ModelName" VirtualizingPanel.CacheLength="3"></ListBox>
<StackPanel Margin="3" Grid.Row="1" Orientation="Horizontal">
<Button Margin="3" Grid.Row="1" Name="btnGetProducts" Click="btnGetProducts_Click_1">GetProducts</Button>
<Button Margin="3" Grid.Row="1" Name="btnDelete" Click="btnDelete_Click_1">Delete</Button>
<Label VerticalAlignment="Center">UnitCost></Label>
<TextBox Width="200" Name="txtMinUnitCost">0</TextBox>
</StackPanel>
<Grid Grid.Row="2" Name="gridProduct" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Margin="3">Model Number:</Label>
<TextBox Name="txtModelNumber" Margin="3" Grid.Column="1" Text="{Binding Path=ModelNumber, Mode=OneWay}" Grid.ColumnSpan="2"></TextBox>
<Label Margin="3" Grid.Row="1">Model Name:</Label>
<TextBox Name="txtModelName" Margin="3" Grid.Column="1" Grid.Row="1" Text="{Binding Path=ModelName, Mode=OneWay}" Grid.ColumnSpan="2"></TextBox>
<Label Margin="3" Grid.Row="2">Unit Cost:</Label>
<TextBox Name="txtUnitCost" Margin="3" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Validation.Error="txtUnitCost_Error_1">
<TextBox.Text>
<Binding Path="UnitCost" NotifyOnValidationError="True">
<Binding.ValidationRules>
<!--<ExceptionValidationRule></ExceptionValidationRule>-->
<!--<DataErrorValidationRule></DataErrorValidationRule>-->
<local:PositivePriceRule Min="0" Max="1000"></local:PositivePriceRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Margin="3" Grid.Row="3">Description:</Label>
<TextBox Name="txtDescription" Margin="3" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3" Text="{Binding Path=Description, Mode=OneWay}"
TextWrapping="Wrap"></TextBox>
</Grid>
</Grid>
</Window>