Search code examples
c#wpfbackgroundtogglebutton

Change background color disabled togglebutton


I have a toggle button in my Wpf application which is disabled in the code

<ToggleButton IsEnabled="False"/>

How can i change the background of the disabled button.


Solution

  • You can change the background brush of a ToggleButton in the template.

    Right click on your Control and -> Edit a copy.

    Name it and define where the Style should be (Ressource or local) There you see a Style and a Template

    Sth. like this appear:

        <Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" SnapsToDevicePixels="True">
                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                           <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="RenderDefaulted" TargetName="Chrome" Value="True"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="#FFADADAD"/>
                                <Setter Property="Background" Value="green"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    In the Trigger "IsEnabled" can you set the new Brush directly or via Binding Value="{StaticResource MyBrush}"