I'm trying to use a DataTriggerBehavior
from the Behaviors SDK. But it doesn't seem to work with enums... or else I'm doing something wrong.
You can assume that the DataContext
for these examples is something like this (INotifyPropertyChanged
is implemented, but I'm not going to show it here):
public class MyDataClass
{
public MyEnum ItemCommand { get; set; }
public string ItemCommandString { get; set; }
}
public enum MyEnum
{
EnumValue1
}
_Button.DataContext = new MyDataClass() { ItemCommand = MyEnum.EnumValue1,
ItemCommandString = "EnumValue1" };
Here is the code that doesn't work (trying to specify an enum value and check against the ItemCommand enum property):
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommand}"
Value="EnumValue1">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>
and this code (checking against an enum resource) also does not work:
<UserControl.Resources>
<local:MyEnum x:Key="_MyEnumValue">EnumValue1</local:MyEnum>
</UserControl.Resources>
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommand}"
Value="{StaticResource _MyEnumValue}">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>
whereas this code (checking against a string) does work:
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommandString}"
Value="EnumValue1">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>
What is the correct way to specify the enum value in the DataTriggerBehavior
Value
property so that this will work?
I was investigating this issue and narrowed the problem down to
TypeConverterHelper
class. TypeConverterHelper sourceApparently it doesn’t account for enum types and falls back to some logic which recreates the xaml string for the enum. Parses it as
ContentControl
and passes back its content. Unfortunately during this step it loses the enum type information and subsequent type casting is not valid.If you are working with sources and not just NuGet package you can fix it yourself. Just add another overload of
Convert
method toTypeConverterHelper
:And of course change the call in DataTriggerBehavior Compare method from:
to:
you can write a Converter:
And use it in XAML:
Or bind direct to the string as in your sample. Unfortunately DataTriggerBehavior in WinRT is worse that DataTrigger in Windows Phone 8