代码之家  ›  专栏  ›  技术社区  ›  Deus Vult

UWP设置单选按钮。代码隐藏中的IsChecked导致奇怪/意外行为

  •  1
  • Deus Vult  · 技术社区  · 7 年前

    我的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
            //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.IsChecked 设置为 true before设置为false,下一个设置为true时。 无论FlipView中添加了多少页面。只有设置为true的最后一页的最后一个单选按钮才保留其值。。。

    我在一个全新的WPF项目中尝试了该代码。。。运行正常。。。

    我在一个全新的UWP项目中尝试了该代码。。。并获得所描述的行为。

    我已将单选按钮更改为复选框(仅在示例代码中)。。。在新的UWP和WPF项目上运行良好。

    我不能在我的官方项目中将我的单选按钮更改为复选框,因为我必须更改几乎所有内容,而且解决方案非常大和复杂。

    顺便说一句: 即使我通过for循环遍历根网格,如下所示:

    // 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;
                                        }
                                    }
                                }
                            }
    

    单选按钮的行为没有改变。。。

    有人有主意吗?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Deus Vult    7 年前

    我的同事Sujeetha想出了如何解决我们的问题。。。UWP处理不同于标准的单选按钮。NET有。

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

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

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

    也许它可以帮助别人。

    当做