I have different ViewModels in my FlipView.ItemsSource. This ViewModels containing a rootgrid, which holds some Stackpanels with information and some of the StackPanels containing radiobuttons.
jfyi:
My Design Pattern is mvvm and my FlipView has a template selector (selects the template depending on the type of viewmodel it get's in the ItemsSource).
so I get all information I need from the Database, instantiate all my ViewModels and every ViewModel-instance will run the following code (pretty simple abstraction).
#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
In case of RadioButtons:
only the last Radiobutton will hold it's value. Every Radiobutton.IsChecked
set to true
before is set to false, as soon as the next one is set to true.
And it doesn't matter how many pages are added to the FlipView. Only the last RadioButton of the last Page which was set to true holds it's value...
I've tried that code in a completely fresh WPF-Project... Runs fine...
I've tried that code in a completely fresh UWP-Project... and get the descibed behavior.
I've changed the RadioButtons to CheckBoxes (just in the sample code)... Runs fine on fresh UWP- and WPF-projects.
I can't change my RadioButtons to CheckBoxes in my official Project because I had to change almost everything, and the solution is pretty big and complex.
btw: even if I iterate through my RootGrids via for-loops like this:
// 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;
}
}
}
}
No changes in behavior for RadioButtons...
Did someone have an idea?
Thank You!
my coworker Sujeetha figured out how to solve our problem... UWP handles the RadioButtons different than standard .NET does.
We have to set a GroupName for them, because UWP is priorizing the GroupName higher than the name of the StackPanel or something like this.
She sent me this link as a reference:
https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/radio-button
May be it may help someone.
Regards