![](/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.