从treeView c#获取项目 [英] get item from treeView c#

查看:74
本文介绍了从treeView c#获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TreeView

I have a TreeView

<TreeView Name="files" Margin="0,0,569,108" Grid.Row="1" ItemsSource="{Binding s1}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Members}" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <CheckBox Name="CheckBox111" Checked="FileCheckBox_Checked" Unchecked="FileCheckBox_Unchecked">
                        <ContentPresenter>
                            <ContentPresenter.Content>
                                <StackPanel Orientation="Horizontal">
                                     <Image Source="file.jpg" Margin="5,0,5,0" Width="20" Height="20" />
                                     <TextBlock Text="{Binding Name}" />
                                 </StackPanel>
                             </ContentPresenter.Content>
                         </ContentPresenter>
                     </CheckBox>
                 </DataTemplate>
             </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>

    </TreeView.ItemTemplate>
</TreeView>

并且我想检查后面代码中的所有复选框:

and i want to check all the checkBoxs in the code-behind:

private void AllFilesCheckBox_Checked(object sender, RoutedEventArgs e)
{
    foreach (Object item in (files as ItemsControl).Items)
    {
       TreeViewItem t = files.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

       foreach (Object item1 in t.Items)
       {
          // TreeViewItem t2 = item1 as TreeViewItem;
         //  CheckBox t1 =item1 as CheckBox;
       }
   }
}

但是我无法访问该复选框...我不知道如何访问它.

But I can not get access to the checkBox... I do not know how to get access to it.

谢谢.

我尝试了almulo的答案,但我几乎明白了. 我有这个:

i tried the almulo's answer and i almost get it. i have this:

我想浏览内容(右边的红色标记),但是我没有孩子.Content

i want get to the content (marks in red in right), but i haven't child.Content

推荐答案

尝试一下,

public static 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 CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        var test = FindVisualChildren<TreeViewItem>(tvEmps);
        List<TreeViewItem> objtreeList = new List<TreeViewItem>();
        foreach (var item in test)
        {
            var chec = FindVisualChildren<CheckBox>(item as TreeViewItem).Cast<CheckBox>();
            if ((chec.FirstOrDefault() as CheckBox).IsChecked == true)
            {
                var textblock = FindVisualChildren<TextBlock>(item as TreeViewItem);
            }
        }
    }

这篇关于从treeView c#获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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