Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to accomplish having a couple of ToggleButtons act sort of like radio buttons--but with the important difference being that having neither checked is a valid case (only up to one can be checked at a time, they are mutually-exclusive).

Here is some XAML that almost works:

XML
<Window>
    <Grid>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <ToggleButton x:Name="ShowLineGridToggleButton">
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsChecked, ElementName=ShowDotGridToggleButton}" Value="True">
                                    <Setter Property="IsChecked" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                    <Image Source="../Images/ShowLineGrid.png" />
                </ToggleButton>
                <ToggleButton x:Name="ShowDotGridToggleButton">
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsChecked, ElementName=ShowLineGridToggleButton}" Value="True">
                                    <Setter Property="IsChecked" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                    <Image Source="../Images/ShowDotGrid.png" />
                </ToggleButton>
            </ToolBar>
        </ToolBarTray>
    <Grid>
</Window>

Clicking on one button will indeed uncheck the other. Unfortunately what is also happening is that the button clicked does not get checked. The background changes properly when the mouse is moved over it, but when the mouse moves away the button shows as not selected.

Commenting-out the Setters allows the clicked button to be checked. It's as if the Setter is also unchecking the clicked button.

I would rather handle this in XAML than have to resort to implementing in code via event handlers; this would keep everything defined in one place.

Ideas?

TIA!
Posted

1 solution

Hi,

You can just use RadioButton with ToggleButton Template:

XML
<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="RadioButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" >
                         <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"/>
                        </ToggleButton>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <RadioButton Content="One"/>
        <RadioButton Content="Two"/>
        <RadioButton Content="Three"/>
        <RadioButton Content="Four"/>
    </StackPanel>
</Window>
 
Share this answer
 
v2
Comments
Dudi Mizrahi 3-May-14 14:59pm    
brilliant solution!!!! my vote is 5+++!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900