Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF中,针对不同状态实施交通灯颜色的最佳方法是什么';申请表上有什么?_Wpf_Colors_Status - Fatal编程技术网

在WPF中,针对不同状态实施交通灯颜色的最佳方法是什么';申请表上有什么?

在WPF中,针对不同状态实施交通灯颜色的最佳方法是什么';申请表上有什么?,wpf,colors,status,Wpf,Colors,Status,如果应用程序具有一组状态。 例如 人员状态(活动[绿色]、非活动[橙色]、已死亡[红色]) 文档状态(未读[橙色]、已读[绿色]、已删除[红色]) 正如你可以告诉大家的,这两组身份的使用相同的颜色。 你们建议在WPF中实现什么。我正在进行重构,希望确保它是可重用的、可读的和可理解的 对于StatusType(enum?)和color之间的每个映射,我将实现从每个enum到笔刷的ValueConverter 如果要重用颜色,请创建笔刷资源并将资源分配给转换器 public class Pers

如果应用程序具有一组状态。 例如

  • 人员状态(活动[绿色]、非活动[橙色]、已死亡[红色])
  • 文档状态(未读[橙色]、已读[绿色]、已删除[红色])
正如你可以告诉大家的,这两组身份的使用相同的颜色。
你们建议在WPF中实现什么。我正在进行重构,希望确保它是可重用的、可读的和可理解的

对于StatusType(enum?)和color之间的每个映射,我将实现从每个enum到笔刷的ValueConverter

如果要重用颜色,请创建笔刷资源并将资源分配给转换器

public class PersonStatusToBrushConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty WhenActiveProperty =
        DependencyProperty.Register("WhenActive", typeof(Brush), typeof(PersonStatusToBrushConverter),
            new PropertyMetadata(Brushes.Green));

    public static readonly DependencyProperty WhenInactiveProperty =
        DependencyProperty.Register("WhenInactive", typeof(Brush), typeof(PersonStatusToBrushConverter),
            new PropertyMetadata(Brushes.Orange));

    public static readonly DependencyProperty WhenDeceasedProperty =
        DependencyProperty.Register("WhenDeceased", typeof(Brush), typeof(PersonStatusToBrushConverter),
            new PropertyMetadata(Brushes.Red));

    public Brush WhenDeceased
    {
        get { return (Brush) this.GetValue(WhenDeceasedProperty); }
        set { this.SetValue(WhenDeceasedProperty, value); }
    }

    public Brush WhenInactive
    {
        get { return (Brush) this.GetValue(WhenInactiveProperty); }
        set { this.SetValue(WhenInactiveProperty, value); }
    }

    public Brush WhenActive
    {
        get { return (Brush) this.GetValue(WhenActiveProperty); }
        set { this.SetValue(WhenActiveProperty, value); }
    }


    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch((PersonStatus)value)
        {
            case PersonStatus.Active:
                return this.WhenActive;
            case PersonStatus.Inactive:
                return this.WhenInactive;
            case PersonStatus.Deceased:
                return this.WhenDeceased;
            default:
                return DependencyProperty.UnsetValue;
        }
    }

    public object ConvertBack(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
对于文档状态,我将创建一个类似的转换器

要将其与资源一起使用,请执行以下操作:

<UserControl ...
    xmlns:conv="clr-namespace:StatusConverters" >
    <UserControl.Resources>
        <SolidColorBrush x:Key="GreenBrush" Color="Green"/>
        <SolidColorBrush x:Key="OrangeBrush" Color="Orange"/>
        <SolidColorBrush x:Key="RedBrush" Color="Red"/>
        <conv:PersonStatusToBrushConverter 
            x:Key="personStatusConverter"
            WhenActive="{StaticResource GreenBrush}" 
            WhenInactive="{StaticResource OrangeBrush}" 
            WhenDeceased="{StaticResource RedBrush}"/>
        <conv:DocumentStatusToBrushConverter 
            x:Key="documentStatusConverter"
            WhenUnread="{StaticResource GreenBrush}" 
            WhenRead="{StaticResource OrangeBrush}" 
            WhenDeleted="{StaticResource RedBrush}"/>
    </UserControl.Resources>
    <Ellipse 
         Fill="{Binding Status, Converter={StaticResource personStatusToBrushConverter}" 
         Width="50" Height="50" />

我们在许多不同的地方使用值转换器。所以很高兴看到这也可以用这种方式实现。谢谢你的提示!目前我想我会按照你建议的方式来实施。