How can I use enum types in XAML?
There's no need to make life so complicated. Enum is easily convertible to string, so you can use that string value in tirgger.
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="Type_DB">
<Setter Property="Source" Value="/Images/DB.PNG"/>
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="Type_SERVER">
<Setter Property="Source" Value="/Images/SERVER.PNG"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
But if you store an int value in your enum... because you can assign:
NodeType val = (NodeType)619;
then... Yes... you have to use a converter.
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.
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;
}