简体   繁体   English

颜色TextBlock取决于枚举值

[英]Color TextBlock depending on enum value

I'm working on WPF with a MvvM model. 我正在使用MvvM模型开发WPF。

I have a view containing Texblocks. 我有一个包含Texblocks的视图。 It display information about ID (from a document and from a database) : 它显示有关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>

I have an enum containing an error code : 我有一个包含错误代码的枚举:

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

And my model is wrote like this : 我的模型是这样写的:

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); }
}

I want that both of my textblocks displaying Model.Db_SURNAME and Model.Dc_SURNAME are colored in red when ErrorID.HasFlag(errorID.SURNAME_DIFF ) . 我希望当ErrorID.HasFlag(errorID.SURNAME_DIFF )时,两个同时显示Model.Db_SURNAMEModel.Dc_SURNAME文本块都被Model.Dc_SURNAME成红色。 And also for Forname . 并且也为Forname

How can I do ? 我能怎么做 ?

Use a converter that converts your enum to a color like following: 使用将您的枚举转换为如下颜色的转换器:

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)
{
   ....
}

} }

Then you can bind the Foreground Color to your enum using the converter: 然后,您可以使用转换器将前景色绑定到您的枚举:

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

The simplest solution: binding Foreground color by the value of ErrorID 最简单的解决方案:通过ErrorID的值绑定Foreground颜色

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

Model 模型

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); }
}

I'd suggest you to go for DataTrigger on your ErrorID property of your Model . 我建议您在Model ErrorID属性上使用DataTrigger

Try something like this: 尝试这样的事情:

  1. Add a namespace of your enum to your View (Visual Studio will help you get it correct) enum的名称空间添加到View (Visual Studio将帮助您使其正确)

     <UserControl [some other namespaces] xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 
  2. Add styling for your TextBox (you can set it for all of them at once by doing this, or add x:Key to reference style explicitly for each TextBox you want to have it): 为您的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> 

Or go for converter, but I personally prefer that approach. 或选择转换器,但我个人更喜欢这种方法。

If an approach with having that style in UserControl.Resources won't work for you because you are not able to bind like this, just put it in your TextBox.Style tag under your TextBox . 如果在UserControl.Resources具有这种样式的方法对您不起作用,因为您无法像这样进行绑定,只需将其放在TextBox下的TextBox.Style标记中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM