[英]VisualStateManager.GoToState doesn't work with DataTemplate
I have a Listbox with the following DataTemplate
set as ItemTemplate
and I want to change the VisualState
of it using Codebehind. 我有一个列表框,其中以下
DataTemplate
设置为ItemTemplate
并且我想使用Codebehind更改它的VisualState
。
DataTemplate: 的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 这是我用来获取Border的代码(FindChild来自“ 如何在视觉树中找到元素?wp7” )并更改VisualState
var blub = FindChild<Border>(listBox, "asds");
VisualStateManager.GoToState(blub, "AllInfoState", true);
However, GoToStates returns false. 但是,GoToStates返回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:
blub确实是我想要的Border(这是第一个Listboxitem),但是VisualStates似乎可以工作,因为当我使用Blend中的Behaviors时,它们实际上确实在改变:
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!
提前致谢!
This is because Border
is not a Control
, but a FrameworkElement
. 这是因为
Border
不是Control
,而是FrameworkElement
。 VisualStateManager.GoToState(Control control, string stateName, bool useTransitions)
however only works with a Control
. VisualStateManager.GoToState(Control control, string stateName, bool useTransitions)
但是仅适用于Control
。
You can use ExtendedVisualStateManager.GoToElementState(FrameworkElement root, string stateName, bool useTransitions)
which you can find in Microsoft.Expression.Interactivity.Core. 您可以使用在Microsoft.Expression.Interactivity.Core中找到的
ExtendedVisualStateManager.GoToElementState(FrameworkElement root, string stateName, bool useTransitions)
。 I guess this is what Blend uses internally. 我想这就是Blend内部使用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.