wpf datatrigger enum binding
C#
//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>
Also in C#:
- Title
- check if multiple variables are null c#
- Category
- C#
- Title
- c# read file stream
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- c# char array to string
- Category
- C#
- Title
- winforms timer c#
- Category
- C#
- Title
- asp net identity include phone number when registering
- Category
- C#
- Title
- hot to move pobject unity
- Category
- C#
- Title
- using serial port in c#
- Category
- C#
- Title
- insert variables into string c#
- Category
- C#
- Title
- unity string format time
- Category
- C#
- Title
- c# windows grab screenshot
- Category
- C#
- Title
- Execute code every x seconds with Update()
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- c# date
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- unity how to change max fps
- Category
- C#
- Title
- return random from enum
- Category
- C#
- Title
- c# webcam
- Category
- C#
- Title
- c sharp split by newline
- Category
- C#
- Title
- convert double to currency c#
- Category
- C#
- Title
- reverse a string in c#
- Category
- C#
- Title
- c# try catch error
- Category
- C#
- Title
- split string
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- c# length 2d array
- Category
- C#
- Title
- for each property in object c#
- Category
- C#
- Title
- c# string to lowercase
- Category
- C#
- Title
- how to route back to url using Request.Headers["Referer"].ToString() in asp.net core
- Category
- C#
- Title
- list.addrange in c#
- Category
- C#
- Title
- c# discord bot
- Category
- C#
- Title
- hwo to make an array in C#
- Category
- C#
- Title
- install .net sdk ubuntu 20
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- how to select time and date in datetimepicker in c#
- Category
- C#
- Title
- defualtsize UWP c#
- Category
- C#
- Title
- validating file upload asp.net core mvc
- Category
- C#
- Title
- unity jump
- Category
- C#
- Title
- how to get specific length of row in matrix c#
- Category
- C#
- Title
- countdown script in unity
- Category
- C#
- Title
- what data type should be for contact number in asp.net
- Category
- C#
- Title
- create new object from generic c#
- Category
- C#
- Title
- math.pow in C# using loop
- Category
- C#
- Title
- c# console delay
- Category
- C#
- Title
- how to set progress openedge driver name for odbc connection c#
- Category
- C#
- Title
- leantween move ui
- Category
- C#
- Title
- convert int to double c#
- Category
- C#
- Title
- how to trim path in C#
- Category
- C#
- Title
- to list c#
- Category
- C#
- Title
- c# append array
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#
- Title
- dynamically add rows to datagridview c#
- Category
- C#
- Title
- make camera follow character unity 2020
- Category
- C#
- Title
- unity how to end a game with esc
- Category
- C#
- Title
- c sharp add item to dictionary
- Category
- C#
- Title
- convert generic to type c#
- Category
- C#
- Title
- C# events
- Category
- C#
- Title
- c# error "The name 'ViewBag' does not exist in the current context"
- Category
- C#
- Title
- populate combobox from array c#
- Category
- C#
- Title
- windows forms check if form is disposed
- Category
- C#
- Title
- how to add array to list in c#
- Category
- C#
- Title
- unity detect number key
- Category
- C#
- Title
- bitmap to byte array c#
- Category
- C#
- Title
- check which activity in focus in android
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- exception handling c#
- Category
- C#
- Title
- configure 1 to 1 relation ef
- Category
- C#
- Title
- how to declare a string c#
- Category
- C#
- Title
- replace all ponctuation characters c#
- Category
- C#
- Title
- csv to dataset c#
- Category
- C#
- Title
- move character unity
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- unity rigid body variable
- Category
- C#
- Title
- moq raise event
- Category
- C#
- Title
- jumping with character controller unity
- Category
- C#
- Title
- call a function c#
- Category
- C#
- Title
- c# counting lines
- Category
- C#
- Title
- countdown script unity
- Category
- C#
- Title
- unity t-flip flop
- Category
- C#
- Title
- how to update a project to cross target .net core
- Category
- C#
- Title
- run async method parallel c#
- Category
- C#
- Title
- unity simple fps controller
- Category
- C#
- Title
- csharp check if env is development
- Category
- C#
- Title
- c# or
- Category
- C#
- Title
- loop gridcontrol devexpress c#
- Category
- C#
- Title
- odbc command parameters c#
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- c# how to sort a list
- Category
- C#
- Title
- parsing object from text file c#
- Category
- C#
- Title
- c# two dimensional array
- Category
- C#
- Title
- c# catch two exceptions in one block
- Category
- C#
- Title
- s3 upload base64 .net core
- Category
- C#
- Title
- remove items from list c# condition
- Category
- C#
- Title
- iphone
- Category
- C#
- Title
- .net identity seed users and roles
- Category
- C#
- Title
- convert comma separated string to array c#
- Category
- C#
- Title
- uwp roaming data sample
- Category
- C#
- Title
- ionic build
- Category
- C#
- Title
- how to add system.messaging c#
- Category
- C#