Click here to Skip to main content
14,427,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
<StackPanel Orientation="Horizontal">
<RadioButton Visibility="{Binding RadioButton1}" IsChecked="{Binding Button1,Mode=TwoWay}"/>
 </StackPanel>
 <StackPanel Orientation="Horizontal">
<RadioButton Visibility="{Binding RadioButton2}" IsChecked="{Binding Button2,Mode=TwoWay}"/>
 </StackPanel>



I want to RadioButton1 check ==> RadioButton2 is false or
RadioButton2 check ==> RadioButton1 is false.


But this code is not run. RadioButton1 and RadioButton2 same time check.I use convert.cs but not run.
How two radio buttons will not be checked at the same time.

What I have tried:

private bool reserveRadioButton1;

public bool ReserveRadioButton1
{
    get{ return reserveRadioButton1; }
    set{  reserveRadioButton1 =value;

      if(reserveRadioButton1)
     {
         ReserveRadioButton2=false;
     }

}

private bool reserveRadioButton2;

public bool ReserveRadioButton2
{
    get{ return reserveRadioButton2; }
    set{  reserveRadioButton2 =value;

      if(reserveRadioButton2)
     {
         ReserveRadioButton1=false;
     }

}
Posted
Updated 14-Jan-20 2:43am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Potentially two issues here

1. RadioButton1 and RadioButton2 are in different containers - there is the concept of "grouping" radio buttons within a container - only one radio button at a time within that group can be checked. This allows for different groups of radio buttons being able to be used on the same page. E.g.
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="G1" Visibility="{Binding RadioButton1}" IsChecked="{Binding Button1,Mode=TwoWay}"/>
<RadioButton GroupName="G1" Visibility="{Binding RadioButton2}" IsChecked="{Binding Button2,Mode=TwoWay}"/>
<RadioButton GroupName="G2" Visibility="{Binding RadioButton3}" IsChecked="{Binding Button3,Mode=TwoWay}"/>
<RadioButton GroupName="G2" Visibility="{Binding RadioButton4}" IsChecked="{Binding Button4,Mode=TwoWay}"/>
</StackPanel>
In this example either Radiobutton 1 or 2 can be checked (because they are in group G1) as well as either Radiobutton 3 or 4 (because they are both in group G2)

2. Another issue that may be affecting you is because you have using Binding on the Boolean IsChecked. Back in 2010 there was a known issue with this (I don't know if it is still an issue - it would also depend on which version of VS you use - so try step 1 first). A workaround for that problem is described here - WPF Tutorial | RadioButton[^]
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100