Search code examples
wpfenumsbindingtextbox

How to use DataTrigger From Enum property?


So i have this Enum:

public enum Status
{
    Intermediate = 0,
    Valid,
    NotValid
}

My ViewModel:

public class MyData
{
   private Status _status;

   public Status Status 
   {
       get { return _status; }
       set
       {
           _status= value;
           OnPropertyChanged();

       }
}

My TextBox Style:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Status.Valid}" Value="True"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" Value="LightSeaGreen" />
</MultiDataTrigger>

So this Status property is changing and i verify the it become Valid but still my TextBox Background color not changing. This style is define in other ResourceDictionary file. The name of this file is TextBox.xaml.

Other properties works fine, the reason i am asking is that i never try to write Trigger with enum so i even dont know how to do that.


Solution

  • Try this:

    <Condition Binding="{Binding Status}" Value="Valid"/>
    

    Or

    <Condition Binding="{Binding Status}" Value="{x:Static local:Status.Valid}"/>
    

    ...where local is mapped against the CLR namespace of Status.

    xmlns:local="clr-namespace:WpfApplication1"