【问题标题】:Why all the xaml Radio buttons got checked even it is in same group?为什么所有 xaml 单选按钮都被选中,即使它在同一组中?
【发布时间】:2018-01-18 07:23:31
【问题描述】:

在项目控件中,我动态绑定了单选按钮列表。每个单选按钮都与视图模型中的“IsChecked”和“Group”绑定。

在 db 级别的某些特定场景中,Ischecked 对同一组中的多个单选按钮变为 true。 在这种情况下,同一组中的所有单选按钮都被选中。当其他单选通过绑定检查时,它不会取消选中其他单选按钮。

<ItemsControl DataContext="{Binding}" ItemsSource="{Binding TabsList,UpdateSourceTrigger=PropertyChanged}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Margin="5">
                        <RadioButton GroupName="test" IsChecked="{Binding Checked}" Content="{Binding _Header}"></RadioButton>                     
                    </ContentControl >
                </DataTemplate>
            </ItemsControl.ItemTemplate>          
        </ItemsControl>

如果我在绑定后手动检查,它工作正常。

提前感谢您的回复。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您的问题是由设置RadioButton 属性的顺序引起的。

    在这种情况下(类似于您的情况):

    <RadioButton IsChecked="{Binding IsChecked1}" GroupName="{Binding GroupName}" />
    <RadioButton IsChecked="{Binding IsChecked2}" GroupName="{Binding GroupName}" />
    

    IsChecked 属性的绑定在GroupName 属性的绑定之前调用。因此,当设置了IsChecked 属性时,属于同一个RadioButtonGroupName 属性仍然未设置。表示GroupName机制无法应用。

    只需颠倒您在其中声明这两个属性的顺序:

    <RadioButton GroupName="{Binding GroupName}" IsChecked="{Binding IsChecked1}" />
    <RadioButton GroupName="{Binding GroupName}" IsChecked="{Binding IsChecked2}" />
    

    一切都会正常工作,因为当设置了IsChecked 属性时,RadioButton 已经知道它的组名是哪个。

    希望对你有帮助。

    【讨论】:

    • 不,它仍在检查同一组中的两个单选按钮。我的单选按钮在模板绑定中,在项目控件中。
    • @BOBINJOSEPH,我根据您写的信息回答了您的问题。现在,您正在添加更多 - 而不是次要的 - 细节。也许您应该在人们回答之前提供他们,而不是之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2017-02-13
    • 2023-02-03
    相关资源
    最近更新 更多