wpf 使用 ObservableCollection 绑定 TreeView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15593166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding TreeView with a ObservableCollection
提问by manonthemoon
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 :
我正在使用 Visual Studio 2010 在 C# 中制作播放器。我正在寻找一种显示文件播放列表的方法。所以我使用的是 TreeView,我想将它与 ObservableCollection 绑定。我的 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.
如您所见,我有 var 播放列表,里面有元素,里面有媒体列表。
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.
我想在wpf中展示这个对象,我认为最好的是TreeView。我在互联网上找不到绑定 TreeView 和我的 var 播放列表的方法。我不知道如何在我的 WPF 中制作我的 TreeView :S ...我在互联网上读到有些人使用 HierachicalDataTemplate,但我真的不知道该怎么做。
Help please
请帮忙
EDIT I'm trying the post given by David, I put my WPF
编辑我正在尝试大卫给出的帖子,我把我的 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
我有问题,我仍然什么都没有 :S 。我完全喜欢教程:S
采纳答案by David
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
你可以看看这篇文章:WPF Treeview: data binding of异构类型的数据使用 CompositeCollection 类
Note: you need to derive your class from INotifyPropertyChanged interface:
注意:您需要从 INotifyPropertyChanged 接口派生您的类:
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.
在此处查看有关 INotifyPropertyChanged 的更多帮助。
回答by Daniil
At first, you need to provide us an XAML where you tries to bind a collection (I suppose you are using an HierarchicalDataTemplate. As the second, I see that "list of Media" is private. As I know the private properties could not be bind.
首先,您需要向我们提供一个 XAML,您可以在其中尝试绑定一个集合(我想您使用的是HierarchicalDataTemplate。作为第二个,我看到“媒体列表”是私有的。据我所知,私有属性不能是绑定。
回答by Rohit Vats
You cannot bind with fields. You can only bind to Public Propertiesin your class.
您不能与fields. 您只能Public Properties在您的班级中绑定到。
public ObservableCollection<Playlist> PlayLists { get; set; }

