MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.28k stars 2.45k forks source link

Togglebutton background not changing #3438

Closed Moerty closed 5 years ago

Moerty commented 5 years ago

Describe the bug Maybe I'm missing something, but the color of the background when the button "IsChecked" can't be changed?

To Reproduce

        <ToggleButton>
            <ToggleButton.Style>
                <Style TargetType="ToggleButton">
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter Property="Background" Value="Black" />
                        </Trigger>

                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Background" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>

Expected behavior When 'IsChecked' = true, I want to see the background of the button white.

timunie commented 5 years ago

Hello Moerty,

please try to add the BasedOn Property to your style (Like the lines below):

      <ToggleButton>
            <ToggleButton.Style>
                <Style TargetType="ToggleButton" BasedOn="{StaticResource MetroToggleButton}">
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter Property="Background" Value="Black" />
                        </Trigger>

                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Background" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>

I've tried this out and it worked for me.

Best regards,

Tim

Moerty commented 5 years ago

Tried this too, but didn't work. I end up using this: BasedOn="{StaticResource MahApps.Metro.Styles.MetroToggleButton}"

Found here: https://github.com/MahApps/MahApps.Metro/blob/5200e9e57755f30134a58a46d26ca473e781b9ea/src/MahApps.Metro/Styles/Controls.Buttons.xaml#L674