Search code examples
wpftogglebuttontemplatebinding

Help trying to use a ToggleButton in a dictionnary


I am very new to WPF and I am facing a problem where I need help:

My environment is .net 4, VS2010, win 7

I want to define a styled toggle button that I will use from a User Control. When I declare the ToggleButton control in the UserControl I want to give the 2 possible Contents according to the button state.

My question: I don't know how to declare my button with the 2 contents (one when IsChecked=true, one when IsChecked=false), I have included some code I have written that does not compile.

Thank you in advance

...


Solution

  • You should be able to use something like:

          <ToggleButton>
            <ToggleButton.Style>
                <Style TargetType="{x:Type ToggleButton}">
                    <Setter Property="Content">
                        <Setter.Value>
                            <Grid>
                                <TextBlock>Click Me</TextBlock>
                            </Grid>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <Grid>
                                        <TextBlock>Click Me Again</TextBlock>
                                    </Grid>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>