我正在用 C# 编写一个 Windows 8 WinRT 应用程序,并且想动态更改我的 ToggleSwitch
的颜色.我会有多个这样的实例,它们不会都具有相同的颜色。
我试图创建一个继承自 ToggleSwitch
的自定义控件,但我很快了解到我不能这样做,因为 ToggleSwitch
是 sealed
类(class)。我的目标是添加 DependencyProperty
类型 Brush
,然后通过 TemplateBinding
使用该刷子在我的自定义控件中 Template
.
还有其他方法可以实现吗?
最佳答案
你可以在 Blend 中完成
Right Click on the ToggleSwitch > Edit Template > Edit Copy
Blend 将在 <Page.Resource>
中插入大量 XAML .更改 Storyboard.TargetName="SwitchCurtain
属性将改变 Curtain 的颜色。
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="SwitchCurtain">
<DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerBorder">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ToggleSwitchTrackPointerOverBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="SwitchKnob">
<DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="SwitchKnob">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
关于c# - 如何更改 WinRT ToggleSwitch 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433882/