根据枚举值为 TextBlock 着色

Color TextBlock depending on enum value

我正在使用 MvvM 模型开发 WPF。

我有一个包含 Texblocks 的视图。它显示有关 ID 的信息(来自文档和数据库):

<GroupBox Grid.Row="1" Grid.Column="0" Header="ID Informations">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="DataBase surname: "/>
        <TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold"/>
        <TextBlock Text="Document surname: "/>
        <TextBlock Text="{Binding Model.Dc_SURNAME}" FontWeight="Bold"/>
        <TextBlock Text="DataBase forename: "/>
        <TextBlock Text="{Binding Model.Db_FORENAME}" FontWeight="Bold"/>
        <TextBlock Text="Document forename: "/>
        <TextBlock Text="{Binding Model.Dc_FORENAME}" FontWeight="Bold"/>
    </StackPanel>
</GroupBox>

我有一个包含错误代码的枚举:

[Flags]
public enum errorID
{
    OK = 1<<0,
    SURNAME_DIFF = 1 << 1,
    FORENAME_DIFF = 1 << 2
}

而我的模型是这样写的:

private String _db_SURNAME;
public String Db_SURNAME
{
    get { return _db_SURNAME; }
    set { SetProperty(ref _db_SURNAME, value); }
}
[...]

private errorID _errorID;
public errorID ErrorID
{
    get { return _errorID; }
    set { SetProperty(ref _errorID, value); }
}

我希望显示 Model.Db_SURNAMEModel.Dc_SURNAME 的两个文本块在 ErrorID.HasFlag(errorID.SURNAME_DIFF ) 时都显示为红色。还有 Forname.

我该怎么办?

最简单的解决方案:通过 ErrorID

的值绑定 Foreground 颜色

XAML

<TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Foreground={Binding SurNameColor}/>
<TextBlock Text="Document surname: "/>
<TextBlock Text="{Binding Model.Dc_SURNAME}" FontWeight="Bold" Foreground={Binding SurNameColor}/>

型号

private Brush _surNameColor = Brush.Black;
public Brush SurNameColor
{
    get { return _surNameColor; }
    set { SetProperty(ref _surNameColor, value); }
}
private errorID _errorID;
public errorID ErrorID
{
    get { return _errorID; }
    set { 
        if(ErrorID.HasFlag(errorID.SURNAME_DIFF))
            SurNameColor = Brushes.Red;
        SetProperty(ref _errorID, value); }
}

使用转换器将您的枚举转换为如下颜色:

public class ErrorIdColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(!(value is errorID))
             throw new ArgumentException("value not of type errorID");
        if(!(parameteris errorID))
             throw new ArgumentException("parameter not of type errorID");

        errorID error = (errorID)value;
        errorID flag = (errorID)parameter;

        if (error.HasFlag(flag))
        {
            return Brushes.Red;
        }
        ...

        return Brushes.Black;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
   ....
}

}

然后您可以使用转换器将前景颜色绑定到您的枚举:

<TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Foreground={Binding Model.errorId, Converter={StaticRessource errorIDColorConverter}, ConverterParameter={StaticRessource errorID.SURNAME_DIFF}}/>

我建议你在 ErrorID 属性 或 Model 上选择 DataTrigger

尝试这样的事情:

  1. 将您的 enum 的命名空间添加到您的 View(Visual Studio 将帮助您更正)

    <UserControl [some other namespaces] 
                 xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 
    
  2. 为您的 TextBox 添加样式(您可以通过执行此操作一次为所有样式设置样式,或者添加 x:Key 以明确为每个 TextBox 你想拥有它):

    <UserControl.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding [your property to bind]}" Value="{x:Static someName:ErrorID.[value of enum]}">
                    <Setter Property="Foreground" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
            <!-- repeat data triggers for each enum value you want to check !-->
        </Style>
    </UserControl.Resources>
    

或者选择转换器,但我个人更喜欢这种方法。

如果在 UserControl.Resources 中使用该样式的方法对您不起作用,因为您无法像这样绑定,只需将其放在 TextBox.Style 标签下的 [=17] =].