颜色TextBlock取决于枚举值

时间:2018-09-11 08:08:46

标签: c# wpf mvvm textblock

我正在使用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_SURNAME时,显示Model.Dc_SURNAMEErrorID.HasFlag(errorID.SURNAME_DIFF )的两个文本块都被涂成红色。还有Forname

我该怎么办?

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

最简单的解决方案:通过Foreground的值绑定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}/>

模型

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

答案 2 :(得分:0)

我建议您在DataTrigger的{​​{1}}属性中寻求ErrorID的帮助。

尝试这样的事情:

  1. Model的命名空间添加到enum(Visual Studio将帮助您使其正确)

    View
  2. 为您的<UserControl [some other namespaces] xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 添加样式(通过执行此操作,您可以一次为所有样式设置样式,也可以为想要的每个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标记中