set enum on trigger wpf code example

Example: wpf datatrigger enum binding

//This example assumes enum and classes are in the same namespace
//If yours are not, use using statements and dot notation
//(Based on an answer from https://social.msdn.microsoft.com/Forums/vstudio/en-US/d22a01cc-91d7-4099-9f56-9707d475a7a8/textblock-text-set-based-on-enumeration?forum=wpf )

//Code-behind, C#
public enum EnumVals
{
	Excellent,
    Poor
}

public partial class MyUserControl : UserControl
    {
        private MyDataContext myDC;

        public MyUserControl()
        {
            InitializeComponent();
            myDC = new MyDataContext();
            this.DataContext = myDC;
        }
    }
//Helper class
public class MyDataContext : DependencyObject
    {
        public static readonly DependencyProperty CurStateProperty = DependencyProperty.Register("CurState", typeof(EnumVals), typeof(MyDataContext), new UIPropertyMetadata(EnumVals.Excellent));

        public EnumVals CurState
        {
            get => (EnumVals)GetValue(CurStateProperty);
            set => SetValue(CurStateProperty, value);
        }

    }


//XAML file snippet. Using the example of changing the fill of a polygon
//based on the value of the enum
<UserControl.Resources>
        <Style TargetType="{x:Type Polygon}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding CurState}" Value="Excellent">
                    <Setter Property="Fill" Value="LimeGreen"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=CurState}" Value="Poor">
                    <Setter Property="Fill" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>