代码隐藏中的 UWP 设置 RadioButton.IsChecked 导致 strange/unexpected 行为
UWP Setting RadioButton.IsChecked in codebehind results in strange/unexpected behavior
我的 FlipView.ItemsSource 中有不同的 ViewModel。这个 ViewModels 包含一个 rootgrid,它包含一些带有信息的 Stackpanels 和一些包含单选按钮的 StackPanels。
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
如果是单选按钮:
只有最后一个 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;
}
}
}
}
单选按钮的行为没有变化...
有人有想法吗?
谢谢!
我的同事 Sujeetha 想出了解决我们问题的方法...UWP 处理 RadioButtons 的方式与标准 .NET 不同。
我们必须为它们设置一个 GroupName,因为 UWP 对 GroupName 的优先级高于 StackPanel 或类似名称的名称。
她发给我这个 link 作为参考:
https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/radio-button
可能对某人有帮助。
此致
我的 FlipView.ItemsSource 中有不同的 ViewModel。这个 ViewModels 包含一个 rootgrid,它包含一些带有信息的 Stackpanels 和一些包含单选按钮的 StackPanels。
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
如果是单选按钮:
只有最后一个 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;
}
}
}
}
单选按钮的行为没有变化...
有人有想法吗?
谢谢!
我的同事 Sujeetha 想出了解决我们问题的方法...UWP 处理 RadioButtons 的方式与标准 .NET 不同。
我们必须为它们设置一个 GroupName,因为 UWP 对 GroupName 的优先级高于 StackPanel 或类似名称的名称。
她发给我这个 link 作为参考:
https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/radio-button
可能对某人有帮助。
此致