开发者

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

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(OnManualCha开发者_Python百科nged)));

Dependency property callback:

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜