Thursday, February 10, 2011

Enumeration (Enum) in XAML

This post describes how to use C# Enumerations in XAML. There are multiple usages exposing enumerations in XAML.

Download sample application here.

Declare the enumeration outside a class in a namespace XXX.

namespace ApplicationWithEnumeration
{
/// <summary>
///
Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
}

public enum Person
{
None1,
Patient1,
Employee1,
Doctor1,
Nurse1
}
}
Use the namespace in the XAML side.
xmlns:local="clr-namespace:ApplicationWithEnumeration"
- OR -
xmlns:XXX="clr-namespace:****"
Done! now you can use it with below declaration.
{x:Static local:Person.Patient1}"

Example,

<ComboBoxItem Content="Patient" Style="{DynamicResource SimpleComboBoxItem}" Tag="{x:Static local:Person.Patient1}" />
- OR - 
<Label Content="{x:Static local:Person.Patient1}" Margin="52.2,0,68.2,46.04" Style="{DynamicResource SimpleLabel}" VerticalAlignment="Bottom" Background="#006F5A5A"/>

Similarly, you can use it iin ChangePropertyAction as parameter dependency and InvokeCommandAction as CommandParamters.


Folks, if you are aware of other usages, please post in the Comments section. I can append to the post later.

No comments: