user3105491
user3105491

Reputation: 21

Set multiple radio buttons through the code(as checked)

Please I want to know what is the problem in this situation: If I have 2 StackPanels each one contains number of radio buttons

<StackPanel>
    <RadioButton x:Name="RentingCK" Checked="RentingCK_Checked"/>
    <RadioButton x:Name="SellingCK" Checked="SellingCK_Checked"/>
    <RadioButton x:Name="UniversityHostelCK" Checked="UniversityHostelCK_Checked"/>
</StackPanel>
<StackPanel>
    <RadioButton x:Name="male"/>
    <RadioButton x:Name="Female"/>
</StackPanel>

In my code in c# I want to set one of the radio button in each StackPanel

Female.IsChecked = true;
UniversityHostelCK.IsChecked = true;

The button that set is the last button I write it in the code, I mean, If I write my code as previous code above the UniversityHostelsCK set and Female unset and vise verse . I hope you are understand my problem, and you can help me. Thank you.

Upvotes: 0

Views: 1347

Answers (2)

Jim
Jim

Reputation: 2984

U can simply do that inside the xaml with the IsChecked property:

<RadioButton x:Name="UniversityHostelCK" IsChecked="True"/>
<RadioButton x:Name="Female" IsChecked="True"/>

or in backend should work just fine with:

UniversityHostelCK.IsChecked = true;
Female.IsChecked = true;

Upvotes: 0

Sebastian Roether
Sebastian Roether

Reputation: 451

Set the GroupName-Property for OptionButtons to group them together. Right now all your option buttons are in the same group and checking one unchecks all other option buttons

<StackPanel>
    <RadioButton GroupName="GroupName" x:Name="RentingCK" Checked="RentingCK_Checked"/>
    <RadioButton GroupName="GroupName" x:Name="SellingCK" Checked="SellingCK_Checked"/>
    <RadioButton GroupName="GroupName" x:Name="UniversityHostelCK" Checked="UniversityHostelCK_Checked"/>
</StackPanel>
<StackPanel>
    <RadioButton GroupName="Gender" x:Name="male"/>
    <RadioButton GroupName="Gender" x:Name="Female"/>
</StackPanel>

Upvotes: 2

Related Questions