Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use enum types in XAML?

I'm learning WPF and I encountered the following problem:

I have an enum type in another namespace than my XAML:

 public enum NodeType  {     Type_SYSTEM = 1,              // System     Type_DB     = 2,              // Database     Type_ROOT   = 512,            // Root folder     Type_FOLDER = 1024,           // Folder  } 

in my XAML I'd like to trigger an image with an integer

<Image.Style>     <Style TargetType="{x:Type Image}">         <Style.Triggers>             <DataTrigger Binding="{Binding Type}" Value="{NodeType: }">                 <Setter Property="Source" Value="/Images/DB.PNG"/>             </DataTrigger>             <DataTrigger Binding="{Binding Type}" Value="128">                 <Setter Property="Source" Value="/Images/SERVER.PNG"/>             </DataTrigger>         </Style.Triggers>     </Style> </Image.Style> 

Is there a way to get an integer value and compare it with an enum type directly in XAML code?

My enum is in namespace AnotherNamespace.Types

<DataTrigger Binding="{Binding IntegerType}" Value="MyEnumType.Type_DB">     <Setter Property="Source" Value="/Images/SERVER.PNG"/>  </DataTrigger> 
like image 433
davymartu Avatar asked Jan 11 '13 13:01

davymartu


People also ask

Which data type can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

Do enums have types?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

What is enum data type syntax?

Enumeration is a user defined datatype in C language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. Here is the syntax of enum in C language, enum enum_name{const1, const2, ....... };


2 Answers

I had a similar question here, and my end result was to create a generic IValueConverter that passed the enum value I wanted to match in as the ConverterParameter, and it returns true or false depending on if the bound value matches the (int) value of the Enum.

The end result looks like this:

XAML Code:

<DataTrigger Value="True"              Binding="{Binding SomeIntValue,                   Converter={StaticResource IsIntEqualEnumConverter},                  ConverterParameter={x:Static local:NodeType.Type_DB}}"> 

Converter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {     if (parameter == null || value == null) return false;      if (parameter.GetType().IsEnum && value is int)     {         return (int)parameter == (int)value;     }      return false; } 
like image 55
Rachel Avatar answered Sep 18 '22 14:09

Rachel


You just need to make sure that your namespace is accounted-for in your XAML header then you can reference both custom DPs and enum values directly in your markup.

For example I use this code to do just that:

<DataTemplate.Triggers>   <MultiDataTrigger>     <MultiDataTrigger.Conditions>       <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True" />       <Condition Binding="{Binding Type}" Value="{x:Static loc:AppProfileItemType.Custom}" />     </MultiDataTrigger.Conditions>     <MultiDataTrigger.Setters>       <Setter TargetName="PART_Delete" Property="Visibility" Value="{x:Static Visibility.Visible}" />     </MultiDataTrigger.Setters>   </MultiDataTrigger> </DataTemplate.Triggers> 

Note that you can't access DataTriggers in a Style, you need to instead make a DataTemplate or ControlTemplate for that (however, .NET 4 adds the ability to set triggers in styles). You can override the ControlTemplate from a Style like so:

<Style x:Key="MyCustomButtonStyle" TargetType="Button">   <Setter Property="Template">     <Setter.Value>       <ControlTemplate TargetType="Button">         <ContentPresenter />         <ControlTemplate.Triggers>           <!-- Put your DataTriggers here -->         </ControlTemplate.Triggers>       </ControlTemplate>     </Setter.Value>   </Setter> </Style> 

For DataTemplates you want to have bindings to an object, you can simply use a ContentPresenter and set its content to the object you want to display along with a DataTemplate definition to use for display of the object. There's always a way to use DataTriggers it's just not always direct or as simple as using a Style.

like image 27
tpartee Avatar answered Sep 20 '22 14:09

tpartee