我的FlipView.ItemsSource中有不同的ViewModel。这个ViewModels包含一个rootgrid,它包含一些包含信息的Stackpanels和一些包含radiobuttons的StackPanel。
jfyi:
我的设计模式是mvvm,我的FlipView有一个模板选择器(根据它在ItemsSource中获得的视图模型的类型选择模板)。
所以我从数据库中获取所需的所有信息,实例化我的所有ViewModel,每个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
//CheckBox rb1 = new CheckBox() { Content = "yes" };
//CheckBox rb2 = new CheckBox() { Content = "no" };
//CheckBox rb3 = new CheckBox() { Content = "yes" };
//CheckBox rb4 = new CheckBox() { Content = "no" };
//CheckBox rb5 = new CheckBox() { Content = "yes" };
//CheckBox rb6 = new CheckBox() { 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 (CheckBox rb in sp.Children) {
// if (rb.Content.ToString() == "yes") {
// rb.IsChecked = true;
// }
// }
//}
#endregion
对于RadioButtons:
只有最后一个Radiobutton会保持它的价值。设置为Radiobutton.IsChecked
之前的每个true
设置为false,只要下一个设置为true即可。
并且FlipView添加了多少页面并不重要。只有最后一页设置为true的最后一个RadioButton才能保持它的值......
我在一个完全新鲜的WPF项目中尝试过该代码......运行得很好......
我已经在一个全新的UWP项目中尝试了这个代码......并获得了所谓的行为。
我已将RadioButtons更改为CheckBoxes(仅在示例代码中)...在新的UWP和WPF项目上运行良好。
我无法在官方项目中将我的RadioButtons更改为CheckBoxes,因为我必须更改几乎所有内容,而且解决方案非常庞大和复杂。
顺便说一句: 即使我通过像这样的for循环迭代我的RootGrids:
// 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;
}
}
}
}
RadioButtons的行为没有变化......
有人有想法吗?
谢谢!
答案 0 :(得分:0)
我的同事Sujeetha想出了如何解决我们的问题... UWP处理与标准.NET不同的RadioButtons。
我们必须为它们设置GroupName,因为UWP正在将GroupName优先于StackPanel的名称或类似的东西。
她发送此链接作为参考:
https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/radio-button
可能对某人有所帮助。
此致