WPF 中为应用程序中的不同状态实现交通灯颜色的最佳方法是什么?

What is the best way in WPF to implement traffic light colours for different status's in the application?

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

如您所知,这两组状态使用相同的颜色。 你们建议在 WPF 中实现什么。我正在重构过程中,希望确保它是可重用的、可读的和可理解的。

对于 StatusType(枚举?)和颜色之间的每个映射,我将实现一个从每个枚举到画笔的 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" />