Color TextBlock取决于枚举值

问题描述 投票:0回答:3

我正在使用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

我能怎么做 ?

c# wpf mvvm textblock
3个回答
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}}/>

0
投票

我建议你去DataTriggerErrorID财产上找Model

尝试这样的事情:

  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下的TextBox标签中。


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); }
}
© www.soinside.com 2019 - 2024. All rights reserved.