将TreeView与ObservableCollection绑定 [英] Binding TreeView with a ObservableCollection
问题描述
我正在使用Visual Studio 2010在C#中制作播放器。
我正在寻找一种显示文件播放列表的方法。
所以我正在使用TreeView,并且想将它与ObservableCollection绑定。
My ObservableCollection:
公共类Media
{
字符串类型; //视频,音乐或图片
字符串名称; //示例:50-cent.mp3
字符串路径; //文件的路径
}
公共类播放列表
{
字符串名称; //播放列表的名称
ObservableCollection< Media>清单//媒体列表
}
public ObservableCollection< Playlist> playLists = new ObservableCollection< Playlist>();
如您所见,我有var播放列表,里面有元素,里面有元素媒体列表。
我想在wpf中显示该对象,我认为最好的是TreeView。
我在互联网上找不到绑定TreeView和var播放列表的方式。
我不知道如何在WPF中制作TreeView:S ...
我在互联网上读到有人使用HierachicalDataTemplate,但是我真的不知道该怎么做。 / p>
请帮助
编辑
我正在尝试David给出的帖子,我放入我的WPF
< TreeView Grid.Column = 1 Height = 193 HorizontalAlignment = Left Margin = 15,209,0,0 Name = treeView1 VerticalAlignment = Top Width = 120>
< TreeView.Resources>
< HierarchicalDataTemplate DataType = {x:Type src:Playlist} ItemsSource = {Binding list}>
< TextBlock Text = {绑定名称} />
< / HierarchicalDataTemplate>
< DataTemplate DataType = {x:Type src:Media}>
< TextBlock Text = {绑定名称} />
< / DataTemplate>
< /TreeView.Resources>
< / TreeView>
我有问题,但我仍然一无所有:S。
我确实很喜欢本教程:S
如果您希望获得如下所示的效果:
您可以查看此帖子: WPF树视图:使用CompositeCollection类对异构类型的数据进行数据绑定
注意:您需要从INotifyPropertyChanged接口派生您的类:
公共类Media:INotifyPropertyChanged
{
字符串_name;
字符串名称{
get {return _name;}
set {_name = value; OnPropertyChanged( Name);}} // OnPropertyChanged很重要!
...
}
查看有关INotifyPropertyChanged 此处。
I'm making a player in C#, by using Visual Studio 2010. I'm looking for a way to display a playlist of files. So I'm using a TreeView, and I want to bind it with an ObservableCollection. My ObservableCollection :
public class Media
{
string type; // Video, music, or picture
string name; // example : 50-cent.mp3
string path; // The path of the file
}
public class Playlist
{
string name; // The name of the playlist
ObservableCollection<Media> list; // The list of Media
}
public ObservableCollection<Playlist> playLists = new ObservableCollection<Playlist>();
As you see, I have the var playlists, inside there are elements, and inside them, there are list of media.
I want to show this object in the wpf, and I think the best is TreeView. I can't find in internet the way for binding TreeView and my var playlists. I don't know how to make my TreeView in my WPF :S ... I read in internet that some people use HierachicalDataTemplate, but I really don't know how to do that exactly.
Help please
EDIT I'm trying the post given by David, I put my WPF
<TreeView Grid.Column="1" Height="193" HorizontalAlignment="Left" Margin="15,209,0,0" Name="treeView1" VerticalAlignment="Top" Width="120">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type src:Playlist}" ItemsSource="{Binding list}">
<TextBlock Text="{Binding name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type src:Media}">
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
I have problem, I still have nothing :S . I did exactly like the tutorial :S
If you want to have such effects as shown below:
You can have a look at this post: WPF Treeview: data binding of heterogeneous types of data using CompositeCollection class
Note: you need to derive your class from INotifyPropertyChanged interface:
public class Media : INotifyPropertyChanged
{
string _name;
string Name {
get {return _name;}
set { _name=value; OnPropertyChanged("Name");}} //OnPropertyChanged is important!
...
}
See more help on INotifyPropertyChanged here.
这篇关于将TreeView与ObservableCollection绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!