【问题标题】:Checking all the checkboxes at once but not in XAML一次检查所有复选框,但不在 XAML 中
【发布时间】:2017-01-04 16:29:52
【问题描述】:

我正在尝试学习如何更有效地使用复选框,我发现这个示例说明了如何完成我想要完成但我不想在 XAML 中完成的事情。

<CheckBox Content="Do Everything" IsChecked="{Binding DoEverything}" IsThreeState="True"/>
<CheckBox Content="Eat" IsChecked="{Binding DoEat}" Margin="20,0,0,0"/>
<CheckBox Content="Pray" IsChecked="{Binding DoPray}" Margin="20,0,0,0"/>
<CheckBox Content="Love" IsChecked="{Binding DoLove}" Margin="20,0,0,0"/>

所以它的作用是如果选中 1,它会检查所有 3,

除了使用 C# 代码外,我如何实现这一点。

【问题讨论】:

    标签: c# wpf xaml checkbox


    【解决方案1】:

    将所有这些复选框放在一个 staklayout 上并为其命名,让它成为 Container

    然后处理Do everything检查事件如下:

     void check_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox senderCheck = sender as CheckBox;
            if (senderCheck.Checked)
            {
                foreach (var c in Container.Children)
                {
                    CheckBox check = c as CheckBox;
                    if (check != null)
                    {
                        if (radio.Id != senderCheck.Id)
                            check.Checked = true;
    
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      这是一个例子:

      <Window x:Class="WPF.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-commpatibility/2006"
              xmlns:local="clr-namespace:WPFXamDataGrid"
              mc:Ignorable="d"
              Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
      
          <Grid>
              <StackPanel x:Name="Container">
                  <CheckBox Content="Do Everything" IsThreeState="True"/>
                  <CheckBox Content="Eat" Margin="20,0,0,0"/>
                  <CheckBox Content="Pray" Margin="20,0,0,0"/>
                  <CheckBox Content="Love" Margin="20,0,0,0"/>
              </StackPanel>
          </Grid>
      
      </Window>
      
      using System.Collections.Generic;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Media;
      
      namespace WPF
      {
          /// <summary>
          /// Interaction logic for MainWindow.xaml
          /// </summary>
          public partial class MainWindow : Window
          {
              public MainWindow()
              {
                  InitializeComponent();
              }
      
              private void Window_Loaded(object sender, RoutedEventArgs e)
              {
                  foreach (CheckBox check in FindVisualChildren<CheckBox>(Container))
                  {
                      // you can create your cases here
                      // my case is all checkboxes checked
                      check.IsChecked = true;
                  }
              }
      
              public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
              {
                  if (depObj != null)
                  {
                      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                      {
                          DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                          if (child != null && child is T)
                          {
                              yield return (T) child;
                          }
      
                          foreach (T childOfChild in FindVisualChildren<T>(child))
                          {
                              yield return childOfChild;
                          }
                      }
                  }
              }
          }
      }
      

      用例:如果选中一个,则全部三个

      private void Window_Loaded(object sender, RoutedEventArgs e)
                  {
                      foreach (CheckBox check in FindVisualChildren<CheckBox>(Container))
                      {
                          if (check.IsChecked)
                             {
                                 foreach (CheckBox check1 in FindVisualChildren<CheckBox>(Container))
                                 {
                                     check1.IsChecked = true;
                                 }
      
                                 break;
                             }
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-04
        • 2020-03-29
        • 1970-01-01
        • 2019-05-29
        • 2012-04-04
        相关资源
        最近更新 更多