4

I have two ToggleButtons. I want only one of them to be in the Pressed state at any time. So let's say Model ToggleButton is pressed:

I want to achieve the below concepts:

  1. If I press Drawing ToggleButton, the Model ToggleButton will be unpressed and Drawing ToggleButton will go to pressed state.
  2. If press the Model Button which is in the pressed state nothing will happen.

By the way here is all I have done so far :(

<ToggleButton Width="50" Height="23"  Margin="0 0 0 0">Model</ToggleButton>
<ToggleButton Width="50" Height="23"  Margin="0 0 7 0">Drawing</ToggleButton>

Update:

Using the provided link under the comments, I came up with this:

<RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Model" IsChecked="True" />
<RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Drawing" />

Now the above code gives me two buttons, but how can I style these? I know how to style. But I don't know what to style here? I mean I have already filled the style property here how can I style the ToggleButton itself?

enter image description here

Vahid
  • 5,144
  • 13
  • 70
  • 146

2 Answers2

3

Since RadioButton inherits from ToggleButton, you can set ToggleButton style to it and use BasedOn to inherit default style of ToggleButton like this:

<RadioButton GroupName="Test" Width="50" Height="23" Margin="0 0 7 0"
                Content="Model">
    <RadioButton.Style>
        <Style TargetType="ToggleButton"
               BasedOn="{StaticResource {x:Type ToggleButton}}">
            <Setter Property="Background" Value="Red"/>
            <!-- Set other properties here-->
        </Style>
    </RadioButton.Style>
</RadioButton>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
2

According to this answer that DLeh linked in comments, you can do this by styling a RadioButton to use the ToggleButton styles.

<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />

To answer your second question on how to customize the style property for this, you can create another style that inherits from the base ToggleButton style, and use it instead. Like this:

<Style x:Key="CustomToggleButtonStyle" 
       TargetType="{x:Type RadioButton}" 
       BasedOn="{StaticResource {x:Type ToggleButton}}">
    // Custom Style setters here
</Style>

<RadioButton Style="{StaticResource CustomToggleButtonStyle}" />

And of course there's always the option of completely rewriting the entire template yourself from scratch. MSDN has a good examples of a custom ToggleButton Template you could use to start with.

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thank you so much Rachel for the link. It helped me a lot. – Vahid Jul 21 '14 at 18:47
  • @Vahid Np, I use that link a lot when creating my own templates, because there's actually a lot more than goes into a good template than just basic appearances :) – Rachel Jul 21 '14 at 20:12