UWP设置RadioButton.IsChecked in codebehind会导致奇怪/意外的行为
我的FlipView.ItemsSource中有不同的视图模型。此视图包含一个根网格,其中包含一些包含信息的StackPanel和一些包含单选按钮的StackPanel jfyi: 我的设计模式是mvvm,我的FlipView有一个模板选择器(根据它在ItemsSource中获得的viewmodel类型选择模板) 因此,我从数据库中获取所需的所有信息,实例化所有ViewModels,每个ViewModel实例将运行以下代码(非常简单的抽象) 对于单选按钮: 只有最后一个单选按钮将保留其值。每一个UWP设置RadioButton.IsChecked in codebehind会导致奇怪/意外的行为,uwp,radio-button,code-behind,ischecked,Uwp,Radio Button,Code Behind,Ischecked,我的FlipView.ItemsSource中有不同的视图模型。此视图包含一个根网格,其中包含一些包含信息的StackPanel和一些包含单选按钮的StackPanel jfyi: 我的设计模式是mvvm,我的FlipView有一个模板选择器(根据它在ItemsSource中获得的viewmodel类型选择模板) 因此,我从数据库中获取所需的所有信息,实例化所有ViewModels,每个ViewModel实例将运行以下代码(非常简单的抽象) 对于单选按钮: 只有最后一个单选按钮将保留其值。每一个
单选按钮.IsChecked
设置为true
之前设置为false,下一个设置为true。
而且,添加多少页面到FlipView并不重要。只有最后一页的最后一个单选按钮(设置为true)保存其值
我在一个全新的WPF项目中尝试了该代码。。。运行良好
我在一个全新的UWP项目中尝试过这个代码。。。并获得所描述的行为
我已将单选按钮更改为复选框(仅在示例代码中)。。。在新的UWP和WPF项目上运行良好
我不能在我的官方项目中将我的单选按钮更改为复选框,因为我必须更改几乎所有内容,而且解决方案相当大和复杂
顺便说一句:
即使我通过for循环遍历根网格,如下所示:
// For every Child-Element of RootGrid
for (int z = 0; z < RootGrid.Children.Count; z++) {
// Check, if the child-elements type is "BindableRadioGroup"
if ((TheQuestions[i].Antwort != null) && (RootGrid.Children[z].GetType() == typeof(BindableRadioGroup))) {
// If so, iterate though all Child-Radiobuttons
for (int x = 0; x < (RootGrid.Children[z] as BindableRadioGroup).rads.Count; x++) {
// and set the Event
(RootGrid.Children[z] as BindableRadioGroup).rads[x].Checked += new RoutedEventHandler(Rb_Checked);
// aditionally check if the Radiobuttons value (if the radiobutton says "yes" or "no") equals the questions answer
if ((RootGrid.Children[z] as BindableRadioGroup).rads[x].Content.ToString() == TheQuestions[i].Antwort) {
// and set the Radiobutton as checked
(RootGrid.Children[z] as BindableRadioGroup).rads[x].IsChecked = true;
}
}
}
}
//对于RootGrid的每个子元素
对于(int z=0;z
单选按钮的行为没有更改
有人有主意吗
谢谢大家! 我的同事苏吉塔想出了解决问题的办法。。。UWP处理单选按钮的方式不同于标准的.NET 我们必须为它们设置一个组名,因为UWP将组名的优先级高于StackPanel或类似的名称 她将此链接发送给我作为参考: 也许它能帮助某人
关于Hi@Cocaiedew,我已经等了24小时了,因为我可以把自己的问题标记为答案。。。不幸的是,24小时后我就忘了。
// For every Child-Element of RootGrid
for (int z = 0; z < RootGrid.Children.Count; z++) {
// Check, if the child-elements type is "BindableRadioGroup"
if ((TheQuestions[i].Antwort != null) && (RootGrid.Children[z].GetType() == typeof(BindableRadioGroup))) {
// If so, iterate though all Child-Radiobuttons
for (int x = 0; x < (RootGrid.Children[z] as BindableRadioGroup).rads.Count; x++) {
// and set the Event
(RootGrid.Children[z] as BindableRadioGroup).rads[x].Checked += new RoutedEventHandler(Rb_Checked);
// aditionally check if the Radiobuttons value (if the radiobutton says "yes" or "no") equals the questions answer
if ((RootGrid.Children[z] as BindableRadioGroup).rads[x].Content.ToString() == TheQuestions[i].Antwort) {
// and set the Radiobutton as checked
(RootGrid.Children[z] as BindableRadioGroup).rads[x].IsChecked = true;
}
}
}
}