none
WPF DataTrigger and Enums?

    Question

  • 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?

    Monday, November 07, 2005 10:20 PM

Answers

  • 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:12 PM

All replies

  • 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:12 PM
  • You 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.
    Monday, November 07, 2005 11:21 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, November 08, 2005 1:12 PM
  •  

    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
    Tuesday, December 20, 2005 4:03 AM
  • 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!

    Wednesday, December 28, 2005 2:49 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}">

    Friday, August 05, 2011 1:11 PM
  • 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}">

    Thursday, August 09, 2012 7:15 AM