none
为什么ItemContainerGenerator.ContainerFromIndex返回值为null? RRS feed

  • 问题

  • 我定义了一个部门类和职员类,其中每个部门包含若干职员,然后在TreeView中显示。显示很正常,但是用ItemContainerGenerator.ContainerFromIndex()方法,只能返回部门节点的TreeViewItem,而职员类的节点返回的都是null,不知是什么原因?(已将所有节点全部展开)。

    执行界面如下:

    代码如下:

    <Window x:Class="test01.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:test01"
            Title="MainWindow" Height="382" Width="291">
        <Window.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Department}" ItemsSource="{Binding Path=Employees}">
                <TextBlock Text="{Binding Path=DepName}" FontWeight="Bold" Foreground="#FF1515E8" />
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type local:Employee}" >
                <TextBlock Text="{Binding Path=EmpName}" />
            </DataTemplate>
        </Window.Resources>
        <Grid Name="grid">     
            <TreeView Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged"  />
        </Grid>
    </Window>

    namespace test01 {

    public class Employee
        {
            public string EmpNo
            { get; set; }
            public string EmpName
            { get; set; }
        }

        public class Department
        {
            public string DepCode
            { get; set; }
            public string DepName
            { get; set; }
            private List<Employee> employees = new List<Employee>();
            public List<Employee> Employees
            {
                get { return employees; }
            }
        }

    public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Department dep1 = new Department(); dep1.DepCode = "dep01"; dep1.DepName = "行政部"; dep1.Employees.Add(new Employee { EmpNo = "001", EmpName = "张三" }); dep1.Employees.Add(new Employee { EmpNo = "002", EmpName = "李四" }); Department dep2 = new Department(); dep2.DepCode = "dep02"; dep2.DepName = "财务部"; dep2.Employees.Add(new Employee { EmpNo = "003", EmpName = "王五" }); dep2.Employees.Add(new Employee { EmpNo = "004", EmpName = "马六" }); treeView.ItemsSource = new Department[] { dep1,dep2 }; } private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { ItemContainerGenerator icg = treeView.ItemContainerGenerator; var v0 = icg.ContainerFromIndex(0); //返回TreeViewItem(行政部) var v1 = icg.ContainerFromIndex(1); //返回TreeViewItem(财务部) var v2 = icg.ContainerFromIndex(2); //返回null var v3 = icg.ContainerFromIndex(3); //返回null var v4 = icg.ContainerFromIndex(4); //返回null var v5 = icg.ContainerFromIndex(5); //返回null } } }





    • 已编辑 seewold 2014年6月12日 1:29
    2014年6月11日 2:51

答案

  • 你好,

    因为treeView.ItemContainerGenerator得到的是第一层的TreeViewItem集合, 因此只有2个Item:行政部和财务部,你可以Debug断点查看。

    如果想获得第二层的Item,需要通过第一层的TreeViewItem来得到,注意,需要展开所有节点

    var v01 = ((TreeViewItem)(v0)).ItemContainerGenerator.ContainerFromIndex(0); // 行政部-张三
    var v02 = ((TreeViewItem)(v0)).ItemContainerGenerator.ContainerFromIndex(1); // 行政部-李四
    
    var v11 = ((TreeViewItem)(v1)).ItemContainerGenerator.ContainerFromIndex(0); // 财务部-王五
    var v12 = ((TreeViewItem)(v1)).ItemContainerGenerator.ContainerFromIndex(1); // 财务部-马六

    参考资料:

    #ItemContainerGenerator.ContainerFromIndex Method
    http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.containerfromindex(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月12日 5:41
    版主

全部回复

  • 你好,

    因为treeView.ItemContainerGenerator得到的是第一层的TreeViewItem集合, 因此只有2个Item:行政部和财务部,你可以Debug断点查看。

    如果想获得第二层的Item,需要通过第一层的TreeViewItem来得到,注意,需要展开所有节点

    var v01 = ((TreeViewItem)(v0)).ItemContainerGenerator.ContainerFromIndex(0); // 行政部-张三
    var v02 = ((TreeViewItem)(v0)).ItemContainerGenerator.ContainerFromIndex(1); // 行政部-李四
    
    var v11 = ((TreeViewItem)(v1)).ItemContainerGenerator.ContainerFromIndex(0); // 财务部-王五
    var v12 = ((TreeViewItem)(v1)).ItemContainerGenerator.ContainerFromIndex(1); // 财务部-马六

    参考资料:

    #ItemContainerGenerator.ContainerFromIndex Method
    http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.containerfromindex(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月12日 5:41
    版主
  • 原来是要一级一级的取啊,谢谢。
    2014年6月12日 15:02