我的FlipView中有不同的ViewModels。项目来源。此视图模型包含一个根网格,其中包含一些包含信息的堆栈面板和一些包含单选按钮的堆栈面板。
jfyi:
我的设计模式是mvvm,我的FlipView有一个模板选择器(根据它在ItemsSource中获得的viewmodel类型选择模板)。
因此,我从数据库中获取所需的所有信息,实例化所有ViewModels,每个ViewModel实例将运行以下代码(非常简单的抽象)。
#region Radiobuttons
RadioButton rb1 = new RadioButton() { Content = "yes" };
RadioButton rb2 = new RadioButton() { Content = "no" };
RadioButton rb3 = new RadioButton() { Content = "yes" };
RadioButton rb4 = new RadioButton() { Content = "no" };
RadioButton rb5 = new RadioButton() { Content = "yes" };
RadioButton rb6 = new RadioButton() { Content = "no" };
StackPanel BindableRadioGroup1 = new StackPanel();
BindableRadioGroup1.Children.Add(rb1);
BindableRadioGroup1.Children.Add(rb2);
StackPanel BindableRadioGroup2 = new StackPanel();
BindableRadioGroup2.Children.Add(rb3);
BindableRadioGroup2.Children.Add(rb4);
StackPanel BindableRadioGroup3 = new StackPanel();
BindableRadioGroup3.Children.Add(rb5);
BindableRadioGroup3.Children.Add(rb6);
Grid RootGrid = new Grid();
Grid.SetRow(BindableRadioGroup1, 0);
Grid.SetRow(BindableRadioGroup2, 1);
Grid.SetRow(BindableRadioGroup3, 2);
RootGrid.Children.Add(BindableRadioGroup1);
RootGrid.Children.Add(BindableRadioGroup2);
RootGrid.Children.Add(BindableRadioGroup3);
foreach (StackPanel sp in RootGrid.Children) {
foreach (RadioButton rb in sp.Children) {
if (rb.Content.ToString() == "yes") {
rb.IsChecked = true;
}
}
}
#endregion
#region CheckBoxes
#endregion
如果是单选按钮:
只有最后一个单选按钮将保留其值。每一个
Radiobutton.IsChecked
设置为
true
before设置为false,下一个设置为true时。
无论FlipView中添加了多少页面。只有设置为true的最后一页的最后一个单选按钮才保留其值。。。
我在一个全新的WPF项目中尝试了该代码。。。运行正常。。。
我在一个全新的UWP项目中尝试了该代码。。。并获得所描述的行为。
我已将单选按钮更改为复选框(仅在示例代码中)。。。在新的UWP和WPF项目上运行良好。
我不能在我的官方项目中将我的单选按钮更改为复选框,因为我必须更改几乎所有内容,而且解决方案非常大和复杂。
顺便说一句:
即使我通过for循环遍历根网格,如下所示:
for (int z = 0; z < RootGrid.Children.Count; z++) {
if ((TheQuestions[i].Antwort != null) && (RootGrid.Children[z].GetType() == typeof(BindableRadioGroup))) {
for (int x = 0; x < (RootGrid.Children[z] as BindableRadioGroup).rads.Count; x++) {
(RootGrid.Children[z] as BindableRadioGroup).rads[x].Checked += new RoutedEventHandler(Rb_Checked);
if ((RootGrid.Children[z] as BindableRadioGroup).rads[x].Content.ToString() == TheQuestions[i].Antwort) {
(RootGrid.Children[z] as BindableRadioGroup).rads[x].IsChecked = true;
}
}
}
}
单选按钮的行为没有改变。。。
有人有主意吗?
非常感谢。