C# 颜色文本块取决于枚举值

C# 颜色文本块取决于枚举值,c#,wpf,mvvm,textblock,C#,Wpf,Mvvm,Textblock,我正在使用MvvM模型开发WPF 我有一个包含Texblocks的视图。它显示有关ID的信息(来自文档和数据库): 我希望显示Model.Db\u姓氏和Model.Dc\u姓氏的两个文本块在ErrorID.haslafg(ErrorID.namus\u DIFF)时都显示为红色。也适用于Forname 我该怎么做?最简单的解决方案:通过ErrorID XAML <TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Fo

我正在使用MvvM模型开发WPF

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

我希望显示
Model.Db\u姓氏
Model.Dc\u姓氏
的两个文本块在
ErrorID.haslafg(ErrorID.namus\u DIFF)
时都显示为红色。也适用于
Forname


我该怎么做?

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

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}/>

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

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}/>

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

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}}/>

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

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
属性上选择
DataTrigger

试着这样做:

  • 枚举
    的命名空间添加到
    视图
    (Visual Studio将帮助您更正该命名空间)

  • 或者选择converter,但我个人更喜欢这种方法


    如果在
    UserControl.Resources
    中使用该样式的方法不适合您,因为您无法像这样绑定,只要把它放在你的
    文本框中。Style
    标签在你的
    文本框下

    我建议你在你的
    模型的
    ErrorID
    属性上选择
    数据触发器

    试着这样做:

  • 枚举
    的命名空间添加到
    视图
    (Visual Studio将帮助您更正该命名空间)

  • 或者选择converter,但我个人更喜欢这种方法



    如果在
    UserControl.Resources
    中使用这种样式的方法对您不起作用,因为您无法这样绑定,那么只需将它放在
    TextBox.style
    标记下的
    TextBox

    很抱歉,但是从MVVM的角度来看,绑定到您模型中的颜色是非常糟糕的。它打破了惯例。我将在几分钟内为您提供一个更符合MVVM的答案。@KacperStachowski它到底打破了哪些惯例?请提供一份参考资料。@Clemens可能需要分离关注点吗?我认为viewmodel应该是关于数据的,XAML和代码背后应该是关于表示的。当然绑定到viewmodel中的属性是可行的,但我同意Kasper的观点,这不是最优雅的解决方案。@ChristianMurschall在视图模型中使用颜色字符串并没有违反任何约定(IMO)。你找不到任何有信誉的推荐人说你不应该那样做。这里的人经常把自己的观点或观点与传统混淆。@Clemens:没错。它还得到MVVM的支持:每个视图模型都以视图可以轻松使用的形式提供来自模型的数据。为此,视图模型有时会执行数据转换。也就是说,我仍然更喜欢DataTrigger解决方案,这可能是我个人的喜好。很抱歉,从MVVM的角度来看,绑定到模型中的颜色太糟糕了。它打破了惯例。我将在几分钟内为您提供一个更符合MVVM的答案。@KacperStachowski它到底打破了哪些惯例?请提供一份参考资料。@Clemens可能需要分离关注点吗?我认为viewmodel应该是关于数据的,XAML和代码背后应该是关于表示的。当然绑定到viewmodel中的属性是可行的,但我同意Kasper的观点,这不是最优雅的解决方案。@ChristianMurschall在视图模型中使用颜色字符串并没有违反任何约定(IMO)。你找不到任何有信誉的推荐人说你不应该那样做。这里的人经常把自己的观点或观点与传统混淆。@Clemens:没错。它还得到MVVM的支持:每个视图模型都以视图可以轻松使用的形式提供来自模型的数据。为此,视图模型有时会执行数据转换。也就是说,我仍然更喜欢DataTrigger解决方案,可能是个人喜好。TextBlock.Foreground是画笔,而不是颜色。对不起,你说得对,然后返回一个新画笔而不是颜色。Foreground使用SolidColorBrush我查找了你的解决方案,但当错误为
    errorID.Nastname\u DIFF
    时,它将始终以红色显示。我的问题表达得很糟糕,我想在姓氏id不同时给textblocks姓氏上色,但我想在姓氏不同时给forename上色(我会编辑我的问题)@SébastienKrejci我用:
    if(eCode.hasblag((ErrorCode)参数))
    修改了你的代码,在我的xaml中:
    Converter={StaticResource ErrorIdColorConverter},ConverterParameter={StaticResource errorID.Nastname_DIFF}}
    它可以工作!谢谢你的ExtBlock。前景是画笔,不是颜色。很抱歉,你说得对,然后返回一个新画笔而不是颜色。前景使用SolidColorBrush我寻找了你的解决方案,但当错误为
    errorID时,它将始终以红色显示。姓氏_DIFF
    。我的问题表达得很糟糕,我想在姓氏id不同时给textblocks姓氏上色,但我想在姓氏不同时给forename上色(我会编辑我的问题)@SébastienKrejci我用:
    if(eCode.hasblag((ErrorCode)参数))
    修改了你的代码,在我的xaml中:
    Converter={StaticResource ErrorIdColorConverter},ConverterParameter={StaticResource errorID.Nastname_DIFF}}
    它可以工作!谢谢,这是如何使用标志枚举的?请参见问题中的
    ErrorID.hasblag(ErrorID.namus_DIFF)
    。注意,对于常规枚举,您只需在DataTrigger的值中使用枚举文字字符串,如
    Value=“namus_DIFF”
    。不需要x:Static.How-doe
    <TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Foreground={Binding Model.errorId, Converter={StaticRessource errorIDColorConverter}, ConverterParameter={StaticRessource errorID.SURNAME_DIFF}}/>
    
    <UserControl [some other namespaces] 
                 xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 
    
    <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>