当 ComboBox 出错时,我希望我的 ComboBox 旁边的标签文本变为红色,但是我目前如何设置标签的文本颜色仅在控件的初始加载时更新。当 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;
}
}