wpf MahApps.Metro 更改 ToggleSwitch 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19269998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:49:10  来源:igfitidea点击:

MahApps.Metro change ToggleSwitch style

wpfstylesmahapps.metrotoggleswitch

提问by purbsel

I'm testing wpf applications using MahApp.Metro. Somehow I'm not able to change the style of the ToggleSwitch. I just want to change simple properties like foreground or background of the switch. What am I doing wrong?

我正在使用 MahApp.Metro 测试 wpf 应用程序。不知何故,我无法更改 ToggleSwitch 的样式。我只想更改开关的前景或背景等简单属性。我究竟做错了什么?

Mainwindow.xaml

主窗口.xaml

<Style  x:Key="flyoutToggleSwitchStyle" TargetType="{x:Type Controls:ToggleSwitch}" BasedOn="{StaticResource {x:Type Controls:ToggleSwitch}}">
        <Setter Property="BorderBrush" Value="WhiteSmoke"/>
        <Setter Property="Background" Value="White"/>    
        <Setter Property="Foreground" Value="Yellow"/>
        <Setter Property="OnLabel" Value="Yes"/>  <!--<<<---THIS WORKS!!-->
        <Setter Property="OffLabel" Value="No"/>
    </Style>

<Controls:ToggleSwitch Style="{StaticResource flyoutToggleSwitchStyle}">
    <Controls:ToggleSwitch.Header>
           <TextBlock>
                    Test                            
           </TextBlock>
     </Controls:ToggleSwitch.Header>
 </Controls:ToggleSwitch>

采纳答案by Alex Klaus

Now there is a new ToggleSwitchproperty called SwitchForegroundwhich allows changing the colour for ONposition (tested on v0.14). Example:

现在有一个ToggleSwitch名为的新属性SwitchForeground,它允许更改ON位置的颜色(在 v0.14 上测试)。例子:

<controls:ToggleSwitch SwitchForeground="{StaticResource MyGreen}" />

回答by purbsel

The problem is that in the Mahapps.Metro ToggleSwitch most of the properties can not be changed within a style, because there is no TemplateBinding or Key defined in the original template definition.
So the style can only be changed by creating a new template. For this the ToggleSwitch and the ToggleSwitchButton templates have to be changed.

问题是在 Mahapps.Metro ToggleSwitch 中的大部分属性都不能在一个样式内更改,因为原始模板定义中没有定义 TemplateBinding 或 Key。
因此只能通过创建新模板来更改样式。为此,必须更改 ToggleSwitch 和 ToggleSwitchButton 模板。

Issue on GitHub
Source of the templates

模板的GitHub
Source 上的问题