wpf user control RadioButton preselection

1.8k views Asked by At

I got a very simple user control (visifire charts + datepickers and some radiobuttons to control filtering etc):

<GroupBox Header="Scale" Width="159" HorizontalAlignment="Left" Margin="10,47,0,0"
          Height="38" VerticalAlignment="Top" Name="scalingBox">
    <Canvas Height="16">
        <RadioButton Name="scaleDays" Content="Days" GroupName="g2" Width="47"
                     IsChecked="True" Checked="scale_Change"/>
        <RadioButton Name="scaleHours" Content="Hours" GroupName="g2" Canvas.Left="60"
                     Width="59" Checked="scale_Change"/>
    </Canvas>
</GroupBox>

First bit is that setting IsChecked to true on the first button doesn't do anything when the control is displayed.

Searching for a workaround I exposed some public methods to check the buttons I want from the parent window, but when I have more than one MyControl calling these methods only checks the buttons on one instance where the methods was last called. like this:

myControl1.SetDefaultScale();
myControl2.SetDefaultScale();

only checks the buttons on myControl2, leaving all the buttons in the group unchecked on myControl1.

Any ideas what may be causing this?


And yes, it was the group name that got shared among all the controls... Thanks Aran

3

There are 3 answers

0
Aran Mulholland On BEST ANSWER

i got your code, put it in a new project and on the main window. and the first button came up checked.

RadioButtons have some bugs, first they lose their bindings if you click on them see here. I have had the behaviour you describe where group names are shared between forms, as have some other people. how i get around these issues is to bind each radio button seperately and handle the selection logic myself. (if one is selected set the properties that the others in the 'group' are binding to to be false)

im hoping it gets fixed in the next release. but for the meantime i bind every radio button seperately and dont use group names.

1
Doug Ferguson On

The cause of why only one radio button may be checked on the form is related to the scope of GroupName.

When I tried with two instances of user controls with radio buttons and no GroupName specified, the scope of the radio button effect was limited to the group box inside the user control.

When the same GroupName was specified for the radio buttons in the user control that meant that all of the radio buttons in the window had the same GroupName and are included in the same group. Only one button in the group in the whole window may be checked at one time.

The answer is to remove the GroupName property from the radio buttons and let the natural grouping of the GroupBox take effect.

0
Yudhi On

I have a similar issues with multiple tabs using the same usercontrol. Removed the GroupName and it worked as expected. Doug pointed out that they are shared across all of the created object (wide scope). Got this working by manually set the checked value.