Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software
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>
Posted on Tuesday, May 13, 2008 11:54 AM .NET , WPF | Back to top


Comments on this post: WPF - Changing the content of a ToggleButton when checked

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
thanks, you saved me a bunch of time with this!
Left by hari on Jan 14, 2009 1:33 PM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
Hey.Thanks, It save a lot of time..
Nice article for wpf beginner.
Left by Ravendra Singh on Dec 24, 2009 2:30 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
thanks a lot. it was very annoying
Left by SİNAN on Jun 03, 2010 11:09 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
Not sure what the underlying problem is... but it fixed it for me as well.
Left by Gishu on Nov 07, 2010 3:35 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
Thanks! it really helped :)
Left by Santosh on Feb 11, 2011 4:13 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
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>
Left by Victor on Jun 03, 2011 2:02 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
I tried this and it didn't work for me at all. I have no idea what I'm doing wrong...

<Style x:Key="MyToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="MinWidth" Value="80"/>
<Setter Property="MinHeight" Value="26"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Background" Value="#FFE54C4C"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF8BBA7C"/>
</Trigger>
</Style.Triggers>
</Style>
Left by Unonamous on Jun 21, 2012 4:21 PM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
You need to "show" to Content how it will be changing (change-rule) when some trigger is a rising. So use ContentTemplate like some posted above.

--
Электромонтажные работы, сантехнические работы, быстро, качественно, гарантия 4 года!!!
Left by Mark on Jun 28, 2012 5:55 AM

# re: WPF - Changing the content of a ToggleButton when checked
Requesting Gravatar...
Victor's answer was a simply true. You should use a ContentTemplate. And in the ContentTemplate you should reassign Triggers rules and style propertyes. Thanks!
--
Производство информационных киосков и программного обеспечения.
Left by Информационные киоски on Jan 27, 2013 4:38 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free