我希望ComboBox出現錯誤時,我的ComboBox旁邊的標籤文本變爲紅色,但是我目前如何設置Label的文本顏色只更新控制器的初始負載。如何在ComboBox中的選擇發生變化時驗證標籤?還是有另一種方式來更新標籤的風格?基於另一個控件驗證的樣式標籤
我有以下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;
}
}
偉大的作品,謝謝。 – rwdial 2012-07-17 23:19:57