TreeView入门

时间:2013-11-14 21:01:24

标签: .net wpf treeview

我有一个集合

在第一行中我有两个ListView,其中第二个是第一行的子节点 第一个ListView不会显示与第二个相同的信息。

我想在TreeView中做同样的事情,我无法弄明白。
据我所知,TreeView的所有级别都必须提供相同的信息

我想要的是一个TreeView来查看级别不同的地方

我想要的是第二个ListView在第一个ListView中的位置 不在旁边

如果答案是我可以用MVVM做到这一点那么还有另一个学习MVVM的好理由吗?

我可以在没有MVVM的情况下完成吗?

<Window x:Class="TreeView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:TreeView"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListView x:Name="lvFamilies" Grid.Row="0" Grid.Column="0" 
                  ItemsSource="{Binding}" DisplayMemberPath="Information"/>
        <ListView x:Name="lvChildren" Grid.Row="0" Grid.Column="1" 
                  ItemsSource="{Binding ElementName=lvFamilies, Path=SelectedItem.Children}" DisplayMemberPath="Name"/>
        <TreeView x:Name="tvFamilies" Grid.Row="1" Grid.ColumnSpan="2"
                  ItemsSource="{Binding}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type local:iLimitedTreeView}">
                    <CheckBox IsChecked="True" Click="CheckBox_Click">
                        <TextBlock Text="{Binding Path=Name}"></TextBlock>
                    </CheckBox>
                </HierarchicalDataTemplate>              
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

namespace TreeView
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Family> Families = new List<Family>();
            Families.Add(new Family("uncle enrie", new List<string>() {"cousin sal", "niece all"}));
            Families.Add(new Family("aunt harriet", new List<string>() { "cousin jim", "cousin bob" }));
            Families.Add(new Family("aunt nanacy", new List<string>() { "cousin art", "cousin jhonny", "niece amy", "niece sarah" }));
            lvFamilies.ItemsSource = Families;
            tvFamilies.ItemsSource = Families;
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("CheckBox_Click");
        }
    }
    public class person : iLimitedTreeView
    {
        public string Name { get; set; }
        public person(string name) { Name = name; }
    }
    public class Family : iLimitedTreeView 
    {
        public string Name { get { return Parent.Name; } }
        public person Parent { get; set; }
        public string Information
        {
            get 
            {
                return Name + " " + Children.Count.ToString(); 
            }
        }
        public List<person> Children { get; set; }
        public Family(string parent, List<string> children)
        {
            Parent = new person(parent);
            Children = new List<person>();
            foreach (string s in children) { Children.Add(new person(s)); }
        }
    }
    public interface iLimitedTreeView
    {
        string Name { get; }
    }
}

0 个答案:

没有答案