wpf datatrigger bind to enum code example
Example: wpf datatrigger enum binding
public enum EnumVals
{
Excellent,
Poor
}
public partial class MyUserControl : UserControl
{
private MyDataContext myDC;
public MyUserControl()
{
InitializeComponent();
myDC = new MyDataContext();
this.DataContext = myDC;
}
}
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);
}
}
<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>