在一个item控件中,我动态绑定了单选按钮的列表。每个单选按钮都与视图模型中的“IsChecked”和“Group”绑定。
在数据库级别的某些特定场景中,对于同一组中的多个单选按钮,Ischecked是真的。在这种情况下,同一组中的所有单选按钮都会被选中。当通过绑定选中其他单选按钮时,它不会取消选中其他单选按钮。
<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>如果我在绑定后手动检查,它工作得很好。
提前感谢您的回复。
发布于 2018-01-18 21:39:46
您的问题是由RadioButton属性的设置顺序引起的。
在这种情况下(与您的情况类似):
<RadioButton IsChecked="{Binding IsChecked1}" GroupName="{Binding GroupName}" />
<RadioButton IsChecked="{Binding IsChecked2}" GroupName="{Binding GroupName}" />在绑定GroupName属性之前调用IsChecked属性的绑定。因此,当设置了IsChecked属性时,属于同一RadioButton的GroupName属性仍未设置。这意味着不能应用GroupName机制。
只需颠倒声明这两个属性的顺序:
<RadioButton GroupName="{Binding GroupName}" IsChecked="{Binding IsChecked1}" />
<RadioButton GroupName="{Binding GroupName}" IsChecked="{Binding IsChecked2}" />一切都会正常工作,因为当设置了IsChecked属性时,RadioButton已经知道它的组名是哪个。
我希望它能帮上忙。
https://stackoverflow.com/questions/48315807
复制相似问题