1

我的 FlipView.ItemsSource 中有不同的 ViewModel。这个 ViewModel 包含一个 rootgrid,其中包含一些带有信息的 Stackpanel 和一些包含单选按钮的 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

如果是单选按钮:

只有最后一个 Radiobutton 会保留它的值。之前的每个Radiobutton.IsChecked设置true都设置为 false,只要下一个设置为 true。向 FlipView 添加多少页也没有关系。只有设置为 true 的最后一页的最后一个 RadioButton 才具有它的值...

我已经在一个全新的 WPF 项目中尝试过该代码......运行良好......

我已经在一个全新的 UWP-Project 中尝试了该代码......并获得了描述的行为。

我已将 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 的行为没有变化...

有人有想法吗?

谢谢你!

4

1 回答 1

1

我的同事 Sujeetha 想出了如何解决我们的问题...... UWP 处理的 RadioButtons 与标准 .NET 不同。

我们必须为它们设置一个 GroupName,因为 UWP 将 GroupName 优先于 StackPanel 的名称或类似的名称。

她给我发了这个链接作为参考:

https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/radio-button

也许它可以帮助某人。

问候

于 2018-01-26T10:20:02.960 回答