![](/img/trans.png)
[英]Why doesn't VisualStateManager.GoToState work on a control in WPF?
[英]VisualStateManager.GoToState doesn't work with DataTemplate
我有一个列表框,其中以下DataTemplate
设置为ItemTemplate
并且我想使用Codebehind更改它的VisualState
。
的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>
这是我用来获取Border的代码(FindChild来自“ 如何在视觉树中找到元素?wp7” )并更改VisualState
var blub = FindChild<Border>(listBox, "asds");
VisualStateManager.GoToState(blub, "AllInfoState", true);
但是,GoToStates返回false。 blub确实是我想要的Border(这是第一个Listboxitem),但是VisualStates似乎可以工作,因为当我使用Blend中的Behaviors时,它们实际上确实在改变:
触发器:
<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>
有人知道发生了什么吗? 提前致谢!
这是因为Border
不是Control
,而是FrameworkElement
。 VisualStateManager.GoToState(Control control, string stateName, bool useTransitions)
但是仅适用于Control
。
您可以使用在Microsoft.Expression.Interactivity.Core中找到的ExtendedVisualStateManager.GoToElementState(FrameworkElement root, string stateName, bool useTransitions)
。 我想这就是Blend内部使用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.