2

I have a Listbox with the following DataTemplate set as ItemTemplate and I want to change the VisualState of it using Codebehind.

DataTemplate:

<DataTemplate x:Key="SortedRecommendationTemplate">
    <Border x:Name="asds">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="OnlyNameState">
                    <Storyboard>
                        ...
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="OnlyImageState"/>
                <VisualState x:Name="AllInfoState">
                    <Storyboard>
                        ...
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid>
            ...
        </Grid
    </Border>
</DataTemplate>

This is the code I am using to get the Border (FindChild is from How to find element in visual tree? wp7) and change the VisualState

var blub = FindChild<Border>(listBox, "asds");
VisualStateManager.GoToState(blub, "AllInfoState", true);

However, GoToStates returns false. And blub really is the Border I want (well it is the first Listboxitem) But the VisualStates seem to work because when I use the Behaviors from Blend they actually do change:

The triggers:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
        <ei:GoToStateAction StateName="AllInfoState" TargetObject="{Binding ElementName=asds}"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="MouseLeave">
        <ei:GoToStateAction StateName="OnlyNameState" TargetObject="{Binding ElementName=asds}"/>
    </i:EventTrigger>

Does someone know what's going on? Thanks in advance!

Community
  • 1
  • 1
martinyyyy
  • 1,652
  • 3
  • 21
  • 44

1 Answers1

4

This is because Border is not a Control, but a FrameworkElement. VisualStateManager.GoToState(Control control, string stateName, bool useTransitions) however only works with a Control.

You can use ExtendedVisualStateManager.GoToElementState(FrameworkElement root, string stateName, bool useTransitions) which you can find in Microsoft.Expression.Interactivity.Core. I guess this is what Blend uses internally.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Till
  • 111
  • 1
  • 3