WPF RadioButton 在 UI 中选择,但在代码中显示为 IsChecked == false

发布于 2024-08-18 04:24:19 字数 1391 浏览 11 评论 0原文

我在组框中有一些单选按钮。我随机选择按钮,从视觉角度来看,所有按钮都完美运行,并且每次选择新按钮时都会调用事件处理程序。

现在我有一个依赖属性,当值发生变化时会有回调。当我在这个回调过程中读取任何按钮的 IsChecked 值时,该值是 False,尽管该按钮是视觉上选择的(它们同时都是 false,奇怪的)。调试器还显示所有未选中的按钮。

呼呼,经过基本验证后,我对原因缺乏了解...

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

事件处理程序:

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

依赖属性:

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

依赖属性回调:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-08-25 04:24:19

单选按钮组存在 Microsoft 错误。
如果您有同一用户控件的多个实例,其中每个用户控件都包含一组单选按钮,则当您在一个实例中更改您的选择时,它将从所有其他实例中删除该选择。

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.

花伊自在美 2024-08-25 04:24:19

嗯,逻辑完整!

我使用了 SwitchBox 的两个不同实例,其中一个通常由 XAML 创建,并显示按钮的实际状态。然而,它是由代码创建的第二个(并且保持不变),由依赖属性回调访问。因此,未选择的单选按钮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文