WPF DataTrigger and Enums?
-
Monday, November 07, 2005 10:20 PM
I'm using DataTrigger in that way:
<
DataTrigger Binding="{Binding Path=Source}" Value="1"><
Setter TargetName="tbSource" Property="Foreground" Value="Red" /></
DataTrigger>But what do I have to use as value when my Binding Source is an Enum?
I tried its name e.g. AvalonApplication1.Status.Online and its value 0,1,2 a.s.o. but nothing worked :( Any hint for me?
All Replies
-
Monday, November 07, 2005 11:12 PMModerator
This is how you would use an enum in a DataTrigger:
<
DataTrigger Binding="{Binding}">
<DataTrigger.Value>
<local:TShirtSizes>Medium</local:TShirtSizes>
</DataTrigger.Value>
<Setter TargetName="tb" Property="Foreground" Value="Red" />
</DataTrigger>
You can find a complete sample with this scenario here: http://www.beacosta.com/Forum/DataTriggerEnum.zip
Let me know if this helped.
Thanks,
Bea -
Monday, November 07, 2005 11:21 PMModeratorYou could also create a converter from the enum to/from string and use the name in the value property. This would be best used when you are in control of the enum and can make the converter registered for that type.
-
Tuesday, November 08, 2005 1:12 PM
I had used a ValueConverter before but didn't want to write Converter for e.g. 50 Enums with 200 Items or so ;)
Beas way works best for me. Thank you
-
Tuesday, December 20, 2005 4:03 AM
What about a enum inside a class instead of a namespace?
e.g.
public class TShirt { public enum TShirtSizes { Small, Medium, Large } }I tried <local:TShirt.TShirtSizes> but this gives a compiler error, as does
<local:TShirt><TShirtSizes>Small</TShirtSizes><local:TShirt>
Any suggestions?
Thanks,
Ryan
-
Wednesday, December 28, 2005 2:49 PM
I think you forgot the mapping:
<?Mapping XmlNamespace="CodeMapNS" ClrNamespace="WindowsApplication1"?>
<Page x:Class="MainPage"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
xmlns:local="CodeMapNS"
>You have to set your Class's Namespace in ClrNamespace!
-
Friday, August 05, 2011 1:11 PM
I know this is an old question, but in case somebody find it like me here is a better solution, probably because of new features to wpf.
An example:
<DataTrigger Binding="{Binding Status}" Value="{x:Static Models:Status.Idle}">
-
Thursday, August 09, 2012 7:15 AM
The simpliest way is to use x:statis xaml extension like this one:
<DataTrigger Binding="{Binding Path=Row.Status}" Value="{x:Static enums:PrintTaskStatus.New}">

