使用WPF 4将嵌套控件添加到Visual State中

时间:2011-06-28 17:50:24

标签: wpf visualstatemanager

我想知道是否可以将Visual State(在WPF 4中)应用于嵌套控件。我有一个堆栈面板,其中包含一些我想根据变化状态改变的元素。

<StackPanel x:Name="panPremioRaggiunto">
 <VisualStateManager.VisualStateGroups>
 <VisualStateGroup Name="StatiComuni">
 <VisualState Name="PremioNonRaggiunto" />
 <VisualState Name="PremioRaggiunto">
 <Storyboard>
  <ColorAnimation Storyboard.TargetName="lblPremioRaggiunto" Storyboard.TargetProperty="Foreground" To="Green" />
 </Storyboard>
 </VisualState>
 </VisualStateGroup>
 </VisualStateManager.VisualStateGroups>
 <TextBlock x:Name="lblPremioRaggiunto">TEXT</TextBlock>
</StackPanel>

当我尝试使用此代码更改整个堆栈面板的状态时

VisualStateManager.GoToState(panPremioRaggiunto, "PremioRaggiunto", False)

没有任何反应:名为lblPremioRaggiunto的嵌套文本块不会改变他的颜色。我可以这种方式应用视觉状态吗?

谢谢, 达尼洛。

2 个答案:

答案 0 :(得分:0)

是的,VisualStateManager可以更改任何控件的状态。您的代码必须有其他问题。

答案 1 :(得分:0)

修复了问题:视觉状态是在控件模板外定义的,因此我必须使用 VisualStateManager.GoToElementState 而不是 GoToState