WPF ToggleSwitch:如何添加Win 10风格

时间:2017-03-13 10:08:25

标签: wpf mahapps.metro toggleswitch

我找到了这种风格here并将其添加到我的Window.Resources

<Style x:Key="Custom.ToggleSwitch.Win10"
       BasedOn="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}"
       TargetType="{x:Type Controls:ToggleSwitch}">
    <Setter Property="Padding" Value="0 0 10 0" />
    <Style.Triggers>
        <Trigger Property="ContentDirection" Value="RightToLeft">
            <Setter Property="Padding" Value="10 0 0 0" />
        </Trigger>
    </Style.Triggers>
</Style>

问题是这一行:

BasedOn="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}" 

得到错误:

  

错误103资源“MahApps.Metro.Styles.ToggleSwitch.Win10”可以   没有解决。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

使用NuGet安装MahApp(工具 - &gt; NuGet包管理器 - &gt; Visual Studio中的包管理器控制台):http://mahapps.com/guides/quick-start.html

Styles/Controls.ToggleSwitch.xaml资源字典合并到:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ToggleSwitch.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="Custom.ToggleSwitch.Win10"
               BasedOn="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}"
                TargetType="{x:Type Controls:ToggleSwitch}">
            <Setter Property="Padding" Value="0 0 10 0" />
            <Style.Triggers>
                <Trigger Property="ContentDirection" Value="RightToLeft">
                    <Setter Property="Padding" Value="10 0 0 0" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>

答案 1 :(得分:0)

如果要使用MahApps提供的样式,只需修改ToggleSwitch即可添加Style属性,如下所示:

<Controls:ToggleSwitch Style="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}" />