1

I have some radio buttons in a group box. I select the buttons randomly, and all works perfectly from a visual standpoint and also the event handler is called each time a new button is selected.

Now I have a dependency property with a callback when the value changes. When in this callback procedure I read the IsChecked value of any button, the value is False, in spite the button is visually selected (they are all false at the same time, strange). The debugger also displays all buttons unchecked.

Hu hu, I'm lacking ideas about the reason, after the basic verifications...

<GroupBox>
    <StackPanel>
        <RadioButton x:Name="btNone"
            Content="Disconnected"
            IsChecked="True"
            Checked="OnSelChecked"/>
        <RadioButton x:Name="btManual"
            Content="Manual"
            Checked="OnSelChecked"/>
    </StackPanel>
</GroupBox>

Event handler:

private void OnSelChecked(object sender, RoutedEventArgs e) {
    if (btManual.IsChecked == true) {
        // is called
    }
}

Dependency property:

public static readonly DependencyProperty ManualProperty =
            DependencyProperty.Register("Manual",
            typeof(Position), typeof(SwitchBox),
            new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.AffectsRender,
                new PropertyChangedCallback(OnManualChanged)));

Dependency property callback:

private static void OnManualChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) {
    SwitchBox box = sender as SwitchBox;
    if (box.btManual.IsChecked == true) {
        // never true, why??
    }
}
Mike
  • 63
  • 2
  • 6

2 Answers2

4

There is a Microsoft bug with the radio button group. If you have multiple instances of the same user control where each user control contains a group of radio buttons, when you change your selection in one instance it will remove the selection from all other instances.

sayed saad
  • 41
  • 2
  • Just for future references, here (http://stackoverflow.com/a/17287651/968003) is detailed explanation of the problem with GroupNames – Alex Klaus Jun 25 '13 at 01:18
  • I have been driving myself nuts trying to track down this issue, and here it is, a known bug I'd never known about. Thanks! – Randolpho Mar 08 '17 at 17:38
  • 10 years later and I stumble across this same problem and it hasn't been fixed - thanks, Microsoft! – Nico Oct 08 '20 at 13:45
0

Hum, logic is intact!

I was using two different instances of SwitchBox, one had been created normally by XAML, and displayed the actual status of the buttons. However it was a second created by code (and left unchanged) that was accessed by the dependency property callback. Thus the unselected radio buttons.

Mike
  • 63
  • 2
  • 6