0

当 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;
    }
}
4

2 回答 2

2

根据您的操作方式,每当您的“SelectedItem”属性更改时,您还需要在“Label”属性上引发属性更改。

您可以通过仅使用 xaml 的解决方案来做到这一点,而不必在模型/视图模型(或您绑定的任何东西)上具有额外的属性。如果您有一个组合框,例如:

<ComboBox Name="comb" ItemsSource="{Binding Strings}" SelectedValue="{Binding Stringy,ValidatesOnDataErrors=True}" />

您可以制作如下标签:

<Label Content="Select a string">
    <Label.Style>
        <Style TargetType="Label">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=comb,Path=(Validation.HasError)}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>

或者,如果您希望在多个控件上使用此功能(或者如果您不想在标签的显式样式中使用此样式/触发器),您可以制作一个附加的属性/行为,例如:

public class LabelValidationHelper
{
    public static FrameworkElement GetDetailControl(DependencyObject obj)
    {
        return (FrameworkElement)obj.GetValue(DetailControlProperty);
    }

    public static void SetDetailControl(DependencyObject obj, FrameworkElement value)
    {
        obj.SetValue(DetailControlProperty, value);
    }

    public static readonly DependencyProperty DetailControlProperty = DependencyProperty.RegisterAttached("DetailControl", typeof(FrameworkElement), typeof(LabelValidationHelper), new UIPropertyMetadata(null, OnDetailControlChanged));       

    private static void OnDetailControlChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        if (args.NewValue == null)
            return;

        var label = (Label)sender;

        var style = new Style(typeof(Label), label.Style);

        var binding = new Binding();
        binding.Source = args.NewValue;
        binding.Path = new PropertyPath(Validation.HasErrorProperty);

        var trigger = new DataTrigger();
        trigger.Binding = binding;
        trigger.Value = true;

        var setter = new Setter();
        setter.Property = Label.ForegroundProperty;
        setter.Value = Brushes.Red;

        trigger.Setters.Add(setter);

        style.Triggers.Add(trigger);

        label.Style = style;
    }

}

你可以像这样使用它:

<Label Content="Select a string" example:LabelValidationHelper.DetailControl="{Binding ElementName=comb}" />
于 2012-07-17T23:08:53.323 回答
0

你只能用风格来做到这一点。

<Style TargetType="Label">
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Padding" Value="0,5"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Target).(Validation.HasError)}" Value="True">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Target).(Validation.HasError)}" Value="False">
                <Setter Property="Foreground" Value="Gray" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

并像这样使用它:

<Label Target="{Binding ElementName=NominaalBox}" Content="Nominaal (gram):"/>
<TextBox x:Name="NominaalBox" Text="{Binding Path=NominaalGram, Mode=TwoWay, TargetNullValue='', UpdateSourceTrigger=PropertyChanged}"/>
于 2016-04-28T07:28:19.943 回答