代码之家  ›  专栏  ›  技术社区  ›  A.Pissicat

颜色文本块取决于枚举值

  •  0
  • A.Pissicat  · 技术社区  · 7 年前

    我正在用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_SURNAME ErrorID.HasFlag(errorID.SURNAME_DIFF ) . 也为了 Forname .

    我该怎么办?

    3 回复  |  直到 7 年前
        1
  •  4
  •   A.Pissicat    7 年前

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

    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}}/>
    
        2
  •  0
  •   Kacper Stachowski    7 年前

    我建议你去 DataTrigger 对你 ErrorID 您的财产 Model .

    尝试如下操作:

    1. 添加您的命名空间 enum 对你 View (Visual Studio将帮助您纠正错误)

      <UserControl [some other namespaces] 
                   xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 
      
    2. 为您的 TextBox (您可以通过这样做一次为所有这些设置,或添加 x:Key 为每个对象显式引用样式 文本框 你想拥有它):

      <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 你下面的标签 文本框 .

        3
  •  0
  •   Antoine V    7 年前

    最简单的解决方案:绑定 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); }
    }