在 ObjectAnimationUsingKeyFrames 中使用 TemplateBinding

发布于 2024-10-07 12:37:13 字数 1074 浏览 4 评论 0原文

我尝试设置鼠标悬停在控件上时的背景颜色。我尝试通过视觉状态管理器来做到这一点。我能够运行以下代码:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Control.Background" Storyboard.TargetName="BorderBackground">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <SolidColorBrush Color="#FF123456" />
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

但我不想在模板中设置颜色,而是将其绑定到控件的值。我通过以下方式尝试使用“标签”属性:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Control.Background" Storyboard.TargetName="BorderBackground">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <SolidColorBrush Color="{TemplateBinding Tag}" />
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

并将使用此样式的控件的标签属性设置为“#FF123456”。

但这行不通。它不会给我留下错误消息,它只是不会改变背景颜色。有谁知道,可能是什么问题?

预先感谢,
坦率

I try to set the background color of a control when mouse is over it. I try to do it via the visual state manager. I was able to get the following code running:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Control.Background" Storyboard.TargetName="BorderBackground">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <SolidColorBrush Color="#FF123456" />
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

But i do not want to set the color in the template, but bind it to a value of the control. I tried it with the 'Tag'-property in the following way:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Control.Background" Storyboard.TargetName="BorderBackground">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <SolidColorBrush Color="{TemplateBinding Tag}" />
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

and setting the Tag-Property of the control that is using this style to '#FF123456'.

But this doesn't work. It doesn't left me an error message, it simply doesn't change the backgroundcolor. Anybody knows, what the problem might be?

Thanks in advance,
Frank

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

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

发布评论

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

评论(2

芸娘子的小脾气 2024-10-14 12:37:14

您无法在单个目标上动态执行此操作,但您可以创建另一个目标(例如称为 BorderBackgroundAlt),您可以在其声明中将其背景显式设置为 {TemplateBinding Tag}。

然后,您可以默认折叠 BorderBackgroundAlt,并在动画帧中切换可见性,而不是切换颜色,从而使 BorderBackground 折叠且 BorderBackgroundAlt 可见。

You cannot do it dynamically on a single target, but what you can do is create another target (for example called BorderBackgroundAlt) whose background you can explicitly set to {TemplateBinding Tag} in its declaration.

Then you can have BorderBackgroundAlt collapsed by default, and in your animation frame instead of switching colors, switch visibilities, such that BorderBackground is collapsed and BorderBackgroundAlt is visible.

甜中书 2024-10-14 12:37:13

不幸的是,您尝试的方法在 Silverlight 中不起作用。 VisualStateManager 动画不是显示树的一部分,因此您不能使用绑定。不过,您可以选择,静态资源(又名在资源字典中设置颜色)和基于代码的动画是不同的解决方法(具体取决于您想要做什么)。对于后者,请查看我提出类似问题的线程: 如何在 Silverlight 4 UserControl 中动态设置属性的动画?

Unfortunately the way you're trying to do it won't work in Silverlight. The VisualStateManager animations are not part of the display tree, so you can't use binding. You have options though, StaticResources (aka setting up colours in the resource dictionary) and code based animations are different workarounds (depending on exactly what you want to do). For the latter check out a thread where I asked a similar question: How can I animate a property dynamically in a Silverlight 4 UserControl?

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