Tim Hibbard

CEO for EnGraph software
posts - 626, comments - 1586, trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

WPF - Changing the content of a ToggleButton when checked

I don't think the WPF ToogleButton has a distinct visual difference between it's checked and unchecked state.  So I wanted to change the text displayed when the button was checked.  So I created a trigger that looked at the IsChecked property and changed the content property accordingly.  However, the default template of a ToggleButton changes it's appearance when it is checked, so certain properties are not written as expected.  To fix this, I set the content property in the styles of the toggle button and it worked.  Here is the code:
<ToggleButton Name="showStopsCheckBox">
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Content" Value="Stops"/>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="Trips"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

Print | posted on Tuesday, May 13, 2008 11:54 AM | Filed Under [ .NET WPF ]

Feedback

Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

thanks, you saved me a bunch of time with this!
1/14/2009 1:33 PM | hari
Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

Hey.Thanks, It save a lot of time..
Nice article for wpf beginner.
12/24/2009 2:30 AM | Ravendra Singh
Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

thanks a lot. it was very annoying
6/3/2010 11:09 AM | SİNAN
Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

Not sure what the underlying problem is... but it fixed it for me as well.
11/7/2010 3:35 AM | Gishu
Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

Thanks! it really helped :)
2/11/2011 4:13 AM | Santosh
Gravatar

# re: WPF - Changing the content of a ToggleButton when checked

If you want to use image as a content - the "Content" property don't gives you expected result. You may use "ContentTemplate" property. For example:
<ToggleButton Width="32" Height="32">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>
<Image Source="{StaticResource imgToggle1}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{StaticResource imgToggle2}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
6/3/2011 2:02 AM | Victor
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: