【发布时间】:2012-09-16 02:30:15
【问题描述】:
我想使用我的控件的 Foreground 值作为 ControlTemplate 中 VisualState ColorAnimation 的源。
我的模板定义看起来主要像 ToggleButton 的标准模板,带有一些 mods(标记为 >>):
<Style TargetType="ToggleButton>
<Setter .../>
...
<<< <Setter Property="Foreground" Value="#FF000000"/> >>>
...
<Setter .../>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState .../>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.Target="BackgroundGradient" Storybord.TargetProperty="(Rectangel.Fill).(GradientBrush.GradientStop)[1].(GradientStopColor)" <<< To="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" >>> />
</Storyboard>
</VisualState>
...
...
...
</Style>
...
...
<ToggleButton <<< Foreground="#FFFF0000" >>> ...../>
所以我希望看到动画使用设置的前景色 (#FFFF0000) 作为鼠标悬停动画的一部分,但它什么也没做。 当我在动画定义中写 To="#FFFF0000" 时,我得到了预期的结果,但我想保持动画颜色动态并且为我的应用程序中的每个 ToggleButton 不同。
知道如何解决这个问题吗?
请!
编辑:在尝试通过向 ContentPresenter 添加一个带有 LinearGradientBrush 的新 Rectangle 来实现与上述类似的效果之后,其中一个 GradientStop 应该绑定到 {TemplateBinding Foreground},我现在收到一个错误,可能会启发我的问题的原因“‘Windows.UI.xaml.DependencyProperty’类型的对象无法转换为‘System.Windows.DependencyProperty’类型。” 看起来 {TemplateBinding ...} 产生了错误类型的 DependencyProperty 或 GradientStop 预计 Windows 应用商店应用程序中的类型错误。 然而!有没有办法通过 XAML 中的显式类型转换或任何其他解决方法来克服这个问题?
谢谢
【问题讨论】:
标签: microsoft-metro controltemplate foreground templatebinding coloranimation