Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Binding VisualStateManager to a property... how? RSS

3 replies

Last post Apr 03, 2012 06:09 AM by Weitzhandler

(0)
  • jsobell

    jsobell

    Member

    5 Points

    7 Posts

    Binding VisualStateManager to a property... how?

    Mar 26, 2010 11:39 AM | LINK

    While happily sticking to the MVVM model in all my Silverlight code, I find the association between VisualState and the underlying model a bit strange.

    SL4 gives us the DataStateBehavior behavior, which lets a state be chosen based on a single property, but what if I have a property with more than two states, such as a "Yes","No","Maybe", and I want to bind that to three possible visual states?

    The Blend3 sample behaviors had a DataStateCaseBehavior which let you define a list of values, but I can't see any equivalent in SL4.

    Is there a reason why this is not done? It's a very tidy solution, allowing designers to implement their own complex storyboards based on property values, so what is a 'proper' practise?

     

  • Eugene Akinshin

    Eugene Akinshin

    Member

    177 Points

    62 Posts

    Re: Binding VisualStateManager to a property... how?

    Mar 29, 2010 03:06 AM | LINK

    You should use 3 GoToStateAction with DataTrigger (one for each visual state)

    <i:Interaction.Triggers>
          <ei:DataTrigger Binding="{Binding Model.Prop}" Value="AtTop">
                  <ei:GoToStateAction StateName="TreeAtTop"/>
              </ei:DataTrigger>
          <ei:DataTrigger Binding="{Binding Model.Prop}" Value="AtLeft">
                  <ei:GoToStateAction StateName="TreeAtLeft"/>
              </ei:DataTrigger>
          <ei:DataTrigger Binding="{Binding Model.Prop}" Value="Hidden">
                  <ei:GoToStateAction StateName="TreeCollapsed"/>
              </ei:DataTrigger>
          </i:Interaction.Triggers>
     

     

  • jsobell

    jsobell

    Member

    5 Points

    7 Posts

    Re: Re: Binding VisualStateManager to a property... how?

    Mar 30, 2010 11:04 AM | LINK

    Brilliant, thanks Eugene.

    It's amazing that I had to spend days searching for an obvious seeming requirement that is crucial for MVVM structures, yet I've never seen it mentioned on anyone's blog or article pages.

    Many thanks for that!

  • Weitzhandler

    Weitzhandler

    Member

    124 Points

    101 Posts

    Re: Binding VisualStateManager to a property... how?

    Apr 03, 2012 06:09 AM | LINK

    Eugene Akinshin

    You should use 3 GoToStateAction with DataTrigger (one for each visual state)

    In my scenario it doesn't trigger a change.

    It's just an object that exposes a boolean property, I want to set the color of an element based on that property, the above code doesn't work unless I change the property after it's loaded.

    Is there another xamly way to do it??

    Shimmy