【问题标题】:WPF - Bug allowing multiple selection in a radiobutton groupWPF - 允许在单选按钮组中进行多项选择的错误
【发布时间】:2025-12-10 14:30:02
【问题描述】:

我在绑定到 MVVM / prism 应用程序中的数据库数据的项目控件和数据模板中有多个比率按钮。每组单选按钮相应地用一个名称分组,以便它们是单独的组。

我遇到的问题(并且违反了单选按钮的约定)是您可以在组中选择多个选项。并非所有选项都允许多选。有些人表现得像其他人不应该做的那样。通过 snoop 进行检查时,所有单选按钮都属于同一组,但有多个按钮向 IsChecked 报告 true。

有什么想法吗?

谢谢

编辑 - 代码

XAML

   <StackPanel Grid.Column="0" Margin="10,0,0,10">
                                        <TextBlock Margin="5,5,0,5"
                                                   FontSize="16"
                                                   FontWeight="Bold"
                                                   Foreground="{Binding      Path=ThemeBackground}"
                                                   Text="From" />
                                        <ItemsControl ItemsSource="{Binding Path=InternetItems}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <RadioButton Margin="5"
                                                                 Content="{Binding Path=Title}"
                                                                 GroupName="InternetFrom"
                                                                 IsChecked="{Binding Path=IsSelected}"
                                                                 IsEnabled="{Binding Path=IsEnabled}" />
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>

查看模型

public ObservableCollection<Item> InternetItems
    {
        get
        {
            return
                new ObservableCollection<Item>(
                    _items.Where(x => x.Category == Category.InternetFrom).OrderBy(x => x.DisplayOrder));
        }

    }

编辑 -

问题已解决。每次选择单选按钮时,背后的代码都会启动一个新的可观察集合,从而导致多个数据上下文,而不管单选按钮的组名是否相同

【问题讨论】:

  • 贴出相关代码和XAML。

标签: wpf mvvm radio-button


【解决方案1】:

RadioButton 控件如果共享同一个容器(例如,任何从 PanelContentControl 派生的东西),则它们是互斥的。在您的情况下,ItemsControl 中生成的每个项目都是一个单独的容器,因此按钮不会自动互斥。

例如: 如果你的ItemsControl 是这样设置的,那么按钮是互斥的:

<ItemsControl>
    <RadioButton Content="1" />
    <RadioButton Content="2" />
    <RadioButton Content="3" />
    <RadioButton Content="4" />
</ItemsControl>

但这不是:

<ItemsControl>
    <Grid>
        <RadioButton Content="1" />
    </Grid>
    <Grid>
        <RadioButton Content="2" />
    </Grid>
    <Grid>
        <RadioButton Content="3" />
    </Grid>
    <Grid>
        <RadioButton Content="4" />
    </Grid>
</ItemsControl>

正如 Dean 所说,分配相同的 GroupName 属性将解决您的问题。

<ItemsControl>
    <Grid>
        <RadioButton Content="1"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="2"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="3"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="4"
                        GroupName="Group1" />
    </Grid>
</ItemsControl>

编辑

如果您有多个ItemsControls,您只需在每个ItemsControl 中为RadioButtons 设置不同的GroupName。在这种情况下,范围内的默认样式会派上用场:

<StackPanel>
    <ItemsControl>
        <ItemsControl.Resources>
            <Style TargetType="{x:Type RadioButton}">
                <Setter Property="GroupName"
                        Value="Group1" />
            </Style>
        </ItemsControl.Resources>
        <Grid>
            <RadioButton Content="1" />
        </Grid>
        <Grid>
            <RadioButton Content="2" />
        </Grid>
        <Grid>
            <RadioButton Content="3" />
        </Grid>
        <Grid>
            <RadioButton Content="4" />
        </Grid>
    </ItemsControl>
    <ItemsControl>
        <ItemsControl.Resources>
            <Style TargetType="{x:Type RadioButton}">
                <Setter Property="GroupName"
                        Value="Group2" />
            </Style>
        </ItemsControl.Resources>
        <Grid>
            <RadioButton Content="1" />
        </Grid>
        <Grid>
            <RadioButton Content="2" />
        </Grid>
        <Grid>
            <RadioButton Content="3" />
        </Grid>
        <Grid>
            <RadioButton Content="4" />
        </Grid>
    </ItemsControl>
</StackPanel>

【讨论】:

    【解决方案2】:

    你做错了,同一组内的RadioButtons 是互斥的。如果您使用GroupName 属性将两个或更多RadioButtons 分配给一个组,您将只能从该组中选择一个RadioButton

    RadioButton.GroupName Property

    获取或设置指定哪些 RadioButton 控件的名称 互斥。

    用户可以在每个组中选择一个 RadioButton。

    您真的认为您在 .NET Framework 中发现了一个尚未有人报告且尚未修复的基本错误吗?

    【讨论】:

    • 我认为这会是一些人早在我之前就已经注意到的事情。但是我在同一页面上有多个项目控件(不同的组名),并且我已经阅读了很多关于人们只能在同一视图中的多个组中选择一个的问题。我想知道是否有可能设置一种风格并允许这种风格?抱歉没有代码。我在家,在工作……
    • @user3195554 无论你的问题是什么,我很确定这是your 代码的问题,而不是 WPF。我已经实现了大量基于 RadioButton 的 UI,从未遇到过任何问题。
    • @user3195554 你称它为“WPF Bug”,所以我不确定这个说法是否准确......
    • 抱歉,我的意思是“WPF - 错误...”
    • 将其添加为对 itemscontrol 的追索导致能够选择每个单选按钮
    最近更新 更多