从 List<customClass> WPF 创建树视图

我想TreeView从List<PhonesFromStudents>.


我的自定义课程是 PhonesFromStudents


public class PhonesFromStudents

    public string Name { get; set; }


    public List<string> Phones { get; set; } 

}

XAML 是:


 <TreeView  x:Name="tv_source" Grid.Row="2" Grid.Column="1" Margin="0,5" HorizontalAlignment="Stretch" ItemsSource="{Binding ListStudents}" Background="White" BorderBrush="{x:Null}">

        <TreeView.ItemTemplate>

            <DataTemplate>

                <TextBlock Text="{Binding Name}"  />

            </DataTemplate>

        </TreeView.ItemTemplate>

    </TreeView>

主窗口.cs


 internal MainWindow(List<PhonesFromStudents> list)

    {


        InitializeComponent();

        this.ListStudents = list;

        this.DataContext = this;

    }

示例:2 名学生 Thomas - iPhone8,iPhone6 Lucas - iPhone4s, S8


我想拥有


Thomas

|_____iPhone8

|_____iPhone6


Lucas

|_____iPhone4s

|_____S8

但是我从 UI 得到一个空列表。有人能帮我吗?谢谢


ABOUTYOU
浏览 293回答 6
6回答

LEATH

使用HierarchicalDataTemplate:<TreeView x:Name="tv_source" Grid.Row="2" Grid.Column="1" Margin="0,5"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HorizontalAlignment="Stretch" ItemsSource="{Binding ListStudents}" Background="White" BorderBrush="{x:Null}">&nbsp; &nbsp; <TreeView.Resources>&nbsp; &nbsp; &nbsp; &nbsp; <HierarchicalDataTemplate DataType="{x:Type local:PhonesFromStudents}" ItemsSource="{Binding Phones}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{Binding Name}"&nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </HierarchicalDataTemplate>&nbsp; &nbsp; </TreeView.Resources></TreeView>...并确保这ListStudents是一个公共财产:public List<PhonesFromStudents> ListStudents { get; set; }
打开App,查看更多内容
随时随地看视频慕课网APP