WPF RadioButton selected in UI, but seen by code as IsChecked == false

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-01-15T15:35:13Z Indexed on 2010/06/09 10:02 UTC
Read the original article Hit count: 291

Filed under:
|
|
|

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??
    }
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about radiobutton