如何使用WPF遍历TabControl中的复选框? [英] How to Loop Through CheckBoxes in a TabControl using WPF?

查看:276
本文介绍了如何使用WPF遍历TabControl中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历Tab控件的子元素,以了解哪些复选框被设置为选中状态或未选中状态.我在SO上找到了各种答案,但似乎无法获得执行我所需的代码.到目前为止,这是我所拥有的:

I am trying to loop through child elements of a Tab Control to know what Check Boxes are set to checked or not checked. I have found various answers on SO, but I can't seem to get the code to do what I need. Here is what I have thus far:

foreach (System.Windows.Controls.TabItem page in this.MainWindowTabControl.Items)
{
      if(VisualTreeHelper.GetChild(page, 0).GetType() == typeof(System.Windows.Controls.Grid))
      {
          var grid = VisualTreeHelper.GetChild(page, 0);
          int gridChildCount = VisualTreeHelper.GetChildrenCount(grid);
          for(int i = 0; i < gridChildCount; i++)
          {
              if(VisualTreeHelper.GetChild(grid, i).GetType() == typeof(CheckBox))
              {
                  CheckBox box = (CheckBox)VisualTreeHelper.GetChild(grid, i);
                  if (boxer.IsChecked == true)
                        checkboxes.Add(box);
              }
          }
          //do work
      }
}

最有可能的是,我错误地考虑了VisualTreeHelper类的工作方式.我想我可以通过XAML代码继续工作,以继续深入到Tab控件的各个子级?目前,我在WPF的xaml上的代码如下:

Most likely, I am thinking incorrectly about how the VisualTreeHelper class works. I imagine I can keep working though the XAML Code to keep moving into deeper and deeper children of the Tab Control? Currently, my code on my WPF's xaml looks like this:

<TabControl x:Name="MainWindowTabControl" HorizontalAlignment="Left" Height="470" 
    Margin="0,10,0,0" VerticalAlignment="Top" Width="1384">
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5" Margin="0,-21,0,0">
                <CheckBox Name="testBox" Content="Check Box" 
            HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1293,50,0,0"/>
           </Grid>
        </TabItem>
</TabControl>

所以,我的理解是我必须逐个工作,也就是说,使用VisualTreeHelper来获取Tab控件的子项(选择Tab项),然后获取TabItem的子项(选择网格),然后获取Grid的子级,然后我最终可以遍历这些子级(复选框)以获取所需的信息.如果我弄错了,有人可以解释我要去哪里了吗?

So, my understanding is that I have to work from child to child, meaning, use the VisualTreeHelper to get the Children of the Tab Control (select Tab Item), then get the children of the TabItem (select the grid), then get the children of Grid, and then I can finally loop through the children (checkboxes) to get the information I want. If I am mistaken can someone please explain where I am going wrong?

将复选框XAML更改为正确的代码

Changed Checkbox XAML to the proper code

推荐答案

据我所知,您无需做任何事情就可以从父母那里得到孩子.您可以使用 LogicalTreeHelper 类.它可以让您通过GetChildren方法查询对象.
您的代码应如下所示:
XAML:

As far as my knowledge goes, there is no need to do what you're doing to get the children from a parent. You can use the LogicalTreeHelper class. It will let you query objects through the GetChildren method.
Your code should look like this:
XAML:

<TabControl x:Name="MainWindowTabControl" HorizontalAlignment="Left" 
Margin="0,10,0,0" VerticalAlignment="Top" Height="181" Width="247">
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5" Margin="0,-21,0,0" x:Name="gridChk">
                <CheckBox x:Name="testBox" Content="Check Box" 
        HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,50,0,0"/>
            </Grid>
        </TabItem>
    </TabControl>

C#:

    List<CheckBox> boxesList = new List<CheckBox>();
    //The named Grid, and not TabControl
    var children = LogicalTreeHelper.GetChildren(gridChk); 

    //Loop through each child
    foreach (var item in children)
    {
        var chkCast = item as CheckBox;
        //Check if the CheckBox object is checked
        if (chkCast.IsChecked == true)
        {
            boxesList.Add(chkCast);
        }
    }

这篇关于如何使用WPF遍历TabControl中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆