get item from treeView c#

自闭症网瘾萝莉.ら 提交于 2019-12-08 04:20:06

问题


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.

thank you.

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

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


回答1:


try this code,

 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 AllFilesCheckBox_Checked(object sender, RoutedEventArgs e)
            {
                var test = FindVisualChildren<CheckBox>(tvEmps);
            }



回答2:


First of all, with your code you're only accessing the second level of all the items. Maybe in your case that's enough, but I'd make the method recursive so it works for any tree of any depth.

I use an extension method to look for the actual control, too, so it works even if the CheckBox is nested inside containers or placed among other controls.

private void AllFilesCheckBox_Checked(object sender, RoutedEventArgs e)
{
    var checkBoxes = GetAllCheckBoxes(files);

    foreach (var checkBox in checkBoxes)
    {
        // Do stuff with the CheckBoxes
    }
}

private List<CheckBox> GetAllCheckBoxes(ItemsControl itemsControl)
{
    var list = new List<CheckBox>();

    foreach (var item in itemsControl.Items)
    {
        var itemContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item);
        var checkBox = itemContainer.GetChildOfType<CheckBox>();

        if (checkBox != null)
            list.Add(checkBox);

        if (itemContainer is ItemsControl)
            list.AddRange(GetAllCheckBoxes(itemContainer));
    }

    return list;
}

Add the extension method to a different, static class:

public static T GetChildOfType<T>(this DependencyObject depObj) 
    where T : DependencyObject
{
    if (depObj == null) return null;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = VisualTreeHelper.GetChild(depObj, i);

        var result = (child as T) ?? GetChildOfType<T>(child);
        if (result != null) return result;
    }

    foreach (var child in LogicalTreeHelper.GetChildren(depObj))
    {
        if (child is DependencyObject)
        {
            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
    }

    return null;
}



回答3:


try this,

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


来源:https://stackoverflow.com/questions/31111740/get-item-from-treeview-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!