入力データ検証のエラーStyleをカスタマイズする。二通りの方法を紹介。
<!-- 参考URl:http://d.hatena.ne.jp/Yamaki/20081211/1228983117 -->
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" FontSize="32">
<Window.Resources>
<Style TargetType="TextBox">
<!-- Validation.HasError添付プロパティは、Validation.Errorに何かしらのValidationErrorが追加されている時にTrueを返す。 -->
<!-- このようにTriggerで参照して、エラーStyleをカスタムすることも可能。 -->
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Margin="30" Text="{Binding ID, ValidatesOnExceptions=True}"/>
<Button Margin="30" Content="Focus to me."/>
</StackPanel>
</Window>
<!-- 参考URL:http://d.hatena.ne.jp/Yamaki/20081210/1228885423 -->
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" FontSize="32">
<Window.Resources>
<!-- ここでエラーStyleのカスタムを定義している。 -->
<ControlTemplate x:Key="ErrTmp">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="{TemplateBinding FontSize}" Text="L" FontFamily="Wingdings"
Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<AdornedElementPlaceholder/>
</StackPanel>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<!-- Validation.ErrorTemplateでエラーStyleのカスタマイズができる。 -->
<TextBox Margin="30" Text="{Binding ID, ValidatesOnExceptions=True}" Validation.ErrorTemplate="{StaticResource ErrTmp}"/>
<TextBox Margin="30" Text="{Binding ID, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource ErrTmp}"/>
</StackPanel>
</Window>