Xaml 更改VisualState中的值
我使用ControlTemplate为按钮创建不同的样式。它的目标当然是类型按钮。我可以为不同的VisualState更改按钮的颜色,但它们都是硬类型的,或者它们是对静态资源的引用 例如:Xaml 更改VisualState中的值,xaml,windows-8,windows-store-apps,win-universal-app,Xaml,Windows 8,Windows Store Apps,Win Universal App,我使用ControlTemplate为按钮创建不同的样式。它的目标当然是类型按钮。我可以为不同的VisualState更改按钮的颜色,但它们都是硬类型的,或者它们是对静态资源的引用 例如: <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
在页面上,我放置了一个按钮:
<Button Template="{StaticResource ResourceKey=ButtonDefaultStyle}"
Background="Yellow"
Content="Hello" />
按钮是黄色的,当你按下它时,它会变成蓝色
现在的问题是,如何更改VisualState::Pressed的颜色?对我来说,为每个不同的颜色按钮创建一个ControlTemplate很麻烦
我正在使用Visual Studio 2013 Express更新3。这是针对使用Universal SDK的Windows 8.1和Windows Phone 8.1的。这是一个非常好的问题。我会尝试定义一个附加的笔刷属性并使用
{TemplateBinding(myNamespace:PropertyClass.BrushPropertyName)}
,但我不确定它是否有效。工作方法是使用VisualTreeHelper
在按钮的模板中查找VisualStateManager.VisualStateGroups
(一旦加载),按名称查找组和状态,按索引获取情节提要/动画/关键帧(因为您无法读取情节提要
的目标)并设置值。这是一个有点黑客,但谁知道-也许它会比解析另一个控制模板更快。。。另一个选择可能是尝试摆弄。我最终创建了一个继承按钮的自定义控件。在那个类中,我创建了一组依赖属性,允许我为每个状态设置不同的颜色
按钮的资源字典如下所示:
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=PressedColor}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
页面上的按钮本身如下所示:
<cc:ButtonStandard Style="{StaticResource ButtonStandardStyle}"
Content="Hello Visual Studio"
Background="Red"
PointerOverColor="Blue"
PressedColor="Orange"
DisabledColor="White"/>
PressedColor是ButtonStandard自定义控件类中的依赖项属性。
这是可行的,但我不知道这是否是最好的方法