c# - 如何更改 WinRT ToggleSwitch 颜色?

标签 c# xaml windows-runtime toggleswitch

我正在用 C# 编写一个 Windows 8 WinRT 应用程序,并且想动态更改我的 ToggleSwitch 的颜色.我会有多个这样的实例,它们不会都具有相同的颜色。

我试图创建一个继承自 ToggleSwitch 的自定义控件,但我很快了解到我不能这样做,因为 ToggleSwitchsealed类(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/

相关文章:

C# 创建函数作用域

c# - ObservableCollection 中的 SelectedItem

c# - WPF ControlTemplate 从父资源继承样式

c++ - C++/CX 和 C++/WinRT 可以在同一个项目中使用吗?

c# - Windows服务持续运行

c# - 将数组从 JavaScript 传递到 C# 的最佳方式?

c# - 将行插入 MySQL 数据库的最有效方法

c# - Visual Studio WPF 设计器不使用覆盖的 SystemColors

以StorageFile为参数的C#测试方法

windows - 从另一个 Windows 运行时应用程序检索数据