我有一个默认的treeviewitem TreeView,另一个有dynaic值。但不是拥有TreeViewItem的名称,我有: https://gyazo.com/00563993faf7f5d59bb8ab6870fd428d
整行是:" MyWindowsMediaPlayer.Model.PlaylistDB"
XAML:
<TreeView Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="135">
<TreeViewItem Header="Bibliotheque" IsExpanded="True">
<TreeViewItem Header="Mes Musiques" IsSelected="True"></TreeViewItem>
<TreeViewItem Header="Mes Videos"></TreeViewItem>
<TreeViewItem Header="Mes Images"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Lecteur"></TreeViewItem>
<TreeViewItem Header="Playlist" ItemsSource="{Binding ItemSourceTree}">
</TreeViewItem>
</TreeView>
CS:
private ObservableCollection<PlaylistDB> _itemSourceTree;
public ObservableCollection<PlaylistDB> ItemSourceTree
{
get { return this._itemSourceTree; }
set
{
this._itemSourceTree = value;
this.OnPropertyChanged("ItemSourceTree");
}
}
在初始化时:
this.ItemSourceTree.Add(new PlaylistDB() { NamePlaylist = "I'm a test" });
this.ItemSourceTree.Add(new PlaylistDB() { NamePlaylist = "I'm a test 2 " });
答案 0 :(得分:1)
您的树视图不知道如何显示PlaylistDB对象。
覆盖PlaylistDB的ToString()Methode,如下所示:
public override string ToString()
{
return "NameOfElement";
}
您也可以编辑TreeView资源,如下所述: http://www.wpf-tutorial.com/treeview-control/treeview-data-binding-multiple-templates/
答案 1 :(得分:1)
您必须在DataTemplate
资源下定义TreeView
,以定义UI应如何呈现TreeViewItem绑定ViewModel的模板。
<DataTemplate DataType="{x:Type local:PlaylistDB}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding NamePlaylist }" />
</StackPanel>
</DataTemplate>