Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have 2 radio buttons and I want them appearance same button in WPF by set Style of them is buttons.
<Window.Resources>
    <Style TargetType="RadioButton">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            <Button Background="{TemplateBinding Property=Background}">
            </Button>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
<RadioButton x:Name="rdBlue" removed="Blue" Canvas.Left="42" Canvas.Top="95"
                   Width="25" Height="18"/>
<RadioButton x:Name="rdGreen" removed="Green" Canvas.Left="80" Canvas.Top="95"
                   Width="25" Height="18"/>

But my issue is i can't check which radio button is checked.
this command don't word(it always return false):
C#
string imagePath = "";
                if (rdBlue.IsChecked.Value == true)
                {
                    imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_BLUE;
                }
                else if (rdGreen.IsChecked.Value == true)
                {
                    imagePath = DefineClass.IMAGE_FOLDER_NAME + DefineClass.DEVISION_CHAR + DefineClass.IMAGE_GREEN;
                }


how to check this radio button is checking?
Posted
Updated 21-Jun-15 23:08pm
v2
Comments
CHill60 22-Jun-15 9:39am    
Not sure what the exact solution is but if you remove that template then the code works (although note you do not need == true when checking a bool) so the problem must be in the template you have used

1 solution

Hi,

In your case, you can apply ToggleButton Style to your radio buttons. It gives you WPF button style in appearance and gives radiobutton's checking functionality.

Here is how you can implement this,

Instead of using ControlTemplate, you can bind radiobutton's style property to ToggleButton like following,

XML
<RadioButton Content="Option" style="{StaticResource {x:Type ToggleButton}}" />
 
Share this answer
 
v4

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