Changing Content of ToggleButton with Style.Triggers only shows "System.Windows.Style" on button

Knox 26 Reputation points
2020-10-06T18:00:04.673+00:00

I have a very simple ToggleButton that uses a Trigger to change the content:

<ToggleButton>
    <Style TargetType="{x:Type ToggleButton}">
        <Setter Property="Content" Value="Unchecked" />

        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content" Value="Checked" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ToggleButton>

However, the text inside the ToggleButton always shows: "System.Windows.Style"

Any idea as to why this is?

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2020-10-06T18:44:56.413+00:00

    Try this:

    <ToggleButton >
        <ToggleButton.Style >
            <Style>
                <Setter  Property="ToggleButton.Content" Value="Unchecked" />
                <Style.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter Property="ToggleButton.Content" Value="Checked" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>
    
    0 comments No comments

  2. DaisyTian-1203 11,646 Reputation points
    2020-10-07T02:41:07.787+00:00

    You missed <ToggleButton.Style> between <ToggleButton> and <Style> like this:

    <ToggleButton>  
                <ToggleButton.Style>  
                    <Style TargetType="ToggleButton">  
                        <Setter Property="Content" Value="Unchecked" />  
                        <Style.Triggers>  
                            <Trigger Property="IsChecked" Value="True">  
                                <Setter Property="Content" Value="Checked" />  
                            </Trigger>  
                        </Style.Triggers>  
                    </Style>  
                </ToggleButton.Style>  
            </ToggleButton>  
    

    Or you can do it like:

     <Window.Resources>  
                <Style x:Key="MyToggleButton"  TargetType="{x:Type ToggleButton}">  
                    <Setter Property="Content" Value="Unchecked" />  
                    <Style.Triggers>  
                        <Trigger Property="IsChecked" Value="True">  
                            <Setter Property="Content" Value="Checked" />  
                        </Trigger>  
                    </Style.Triggers>  
                </Style>  
            </Window.Resources>  
            <Grid>  
                <ToggleButton Style="{StaticResource MyToggleButton}"></ToggleButton>  
            </Grid>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.