How to set C# WPF ToggleSwitch control colors? (ModernWpf)

188 views Asked by At

I am using the ToggleSwitch control, which is part of the ModernWpf toolkit within my WPF C# application. I would like to be able override the colours of the control switch but am not sure which property to set to do this. I tried changing the Foreground property, but this only changes the text colour, not the colour of the control switch itself.

In App.xaml I define MahApps.Metro themes.

<ResourceDictionary.MergedDictionaries>
    <ui:ThemeResources>
        <ui:ThemeResources.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </ui:ThemeResources.ThemeDictionaries>
    </ui:ThemeResources>
</ResourceDictionary>

And in MainWindow.xaml I set the theme

xmlns:ui="http://schemas.modernwpf.com/2019"
ui:ThemeManager.RequestedTheme="Dark"

Then in my user control xaml I set the style for toggle switches

xmlns:ui="http://schemas.modernwpf.com/2019"

<Style TargetType="{x:Type ui:ToggleSwitch}">
    <Setter Property="FlowDirection" Value="LeftToRight" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Width" Value="150" />
    <Setter Property="Height" Value="30" />
    <Setter Property="Margin" Value="5,0" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="TextBlock.FontStyle" Value="Normal" />
    <Setter Property="FontWeight" Value="Regular" />
</Style>

Then declare my toggle switch

<ui:ToggleSwitch x:Name="myToggleSwitch" HorizontalAlignment="Left"/>

Which property should I set to change the colour of the control switch?

0

There are 0 answers