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!