ComboBox がエラーになったときに、ComboBox の横にある Label のテキストを赤くしたいと思いますが、現在の設定方法では、コントロールの初期ロード時にのみ Label のテキストの色が更新されます。ComboBox の選択が変更されたときに Label の検証を行うにはどうすればよいですか? または、ラベルのスタイルを更新する別の方法はありますか?
次の XAML があります。
<Style x:Key="labelStyle" TargetType="Label">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder>
<Border BorderBrush="Transparent" BorderThickness="0" />
</AdornedElementPlaceholder>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
<Label Style="{StaticResource labelStyle}" Content="{Binding Path=Label, ValidatesOnDataErrors=True}" />
<ComboBox ItemsSource="{Binding Path=ItemList}" SelectedItem="{Binding Path=SelectedItem, ValidatesOnDataErrors=True}"/>
そして、コードで:
public string this[string propertyName]
{
get
{
if (propertyName == "Label")
{
if (this.IsRequired && !DelayValidation && SelectedItem == "")
return Label + " required";
}
return null;
}
}