使用 WPF 4 将视觉状态添加到嵌套控件

发布于 2024-11-17 14:06:54 字数 893 浏览 5 评论 0原文

我想知道是否可以将视觉状态(在 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 的嵌套文本块不会相应地更改其颜色。我可以以这种方式应用视觉状态吗?

谢谢, 达尼洛.

I'd like to know if is it possible to apply a Visual State (in WPF 4) to nested controls. I've got a stack panel that contains some elements I'd like to change according to variation state.

<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>

When I try to change the state of entire stack panel with this code

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

nothing happens: nested textblock named lblPremioRaggiunto don't change his color according. Can I apply a visual state in this manner?

Thanks,
Danilo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2024-11-24 14:06:54

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

Yes, VisualStateManager can change the state of any control. You must have some other issue with your code.

绾颜 2024-11-24 14:06:54

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

Fixed issue: visual states are defined outside a control template so I have to use VisualStateManager.GoToElementState instead of GoToState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文