基于另一个控件验证的样式标签

时间:2012-07-17 19:31:34

标签: c# wpf validation xaml idataerrorinfo

我希望当ComboBox出错时,我的ComboBox旁边的Label文本变成红色,但是我现在如何设置Label的文本颜色只会更新控件的初始加载。当ComboBox中的选择发生变化时,如何进行Label验证?或者还有另一种更新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;
    }
}

2 个答案:

答案 0 :(得分: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}" />

答案 1 :(得分: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}"/>