![](/img/trans.png)
[英]Bind to an ObservableCollection which is inside another ObservableCollection
[英]How to get an ObservableCollection list item which inside another ObservableCollection list?
我有一個類PlayerVM.cs ,它具有一個屬性:
public ObservableCollection<PlaylistVM> PlayLists
{
get { return _playLists; }
set { _playLists = value; }
}
PlaylistVM.cs也有其自己的集合:
public ObservableCollection<CompositionVM> Songs
{
get
{
return _songs;
}
set
{
_songs = value;
}
}
這樣,我的PlayerVM就有一個播放列表,每個播放列表都有自己的歌曲列表。 我需要顯示按字母順序排序的歌曲列表。 但是我不想更改它們在ObservableCollections中的順序,我只想在GUI中更改它們的順序(僅按字母順序顯示它們,而不更改實際順序)。 我是wpf(和xaml)的新手,今天我只學習了如何顯示按字母順序排列的播放列表名稱。 我正在通過代碼:
<Window x:Class="Player_beta.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Player_beta">
<Window.Resources>
<CollectionViewSource Source="{Binding PlayLists}" x:Key="plView"><!--here I would like to get access to PlayLists.Songs-->
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="plNAME"/> <!--and here I would like to get NAME(that property of 'Songs' which contains songtitle) instead of plNAME - name of playlist)-->
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<!-- etc...-->
<TreeView Name="treeCategories" ItemsSource="{Binding Source={StaticResource plView}}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Songs}">
<TextBlock Text="{Binding plNAME}"/> <!-- <-- here is where the name of PLAYLIST shows-->
<HierarchicalDataTemplate.ItemTemplate >
<DataTemplate >
<TextBlock Text="{Binding NAME}"/> <!-- <-- here is where the name of song shows-->
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
您可以在PlaylistVM.cs類中添加ICollectionView
實例並與其綁定。 這樣,原始列表也不會受到影響。
private ICollectionView songsView;
public ICollectionView SongsView
{
get
{
if (songsView== null)
{
songsView= CollectionViewSource.GetDefaultView(Songs);
songsView.SortDescriptions.Add(new SortDescription("Name",
ListSortDirection.Ascending));
}
return songsView;
}
}
在XAML中:
<HierarchicalDataTemplate ItemsSource="{Binding SongsView}">
您可以做的是,可以將CollectionView
屬性添加到PlaylistVM
類中:
public CollectionView songsCollectionView;
public CollectionView SongsCollectionView
{
get
{
if (songsCollectionView == null)
{
songsCollectionView = new CollectionView(Songs);
songsCollectionView.SortDescriptions.Add(new SortDescription("plNAME", ListSortDirection.Ascending));
}
return songsCollectionView;
}
}
然后您可以像這樣綁定您的xaml:
<HierarchicalDataTemplate ItemsSource="{Binding SongsCollectionView}">
這應該滿足您在GUI中顯示排序后的歌曲而不更改原始集合的要求。
這不是最有效的方法,但是可以完成工作。 請注意,它不會檢測任何歌曲的名稱是否已更改。 每當添加或刪除歌曲時,它還將在UI中重建整個列表。
public class PlaylistVM: System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<CompositionVM> _songs;
public ObservableCollection<CompositionVM> Songs
{
get
{
return _songs;
}
set
{
if(_songs != value)
{
//if old value not null, unhook event
if (_songs != null)
{
_songs.CollectionChanged -= FireSongsChanged;
}
//change the value
_songs = value;
//if new value !=null, then attach handlers.
if (_songs != null)
{
_songs.CollectionChanged += FireSongsChanged;
}
//this will fire the
FireSongsChanged(null, null);
}
}
}
void FireSongsChanged(object sender, EventArgs e)
{
//the collection of songs has changed, tell UI that it needs to requery the list of ordered songs.
var ev = this.PropertyChanged;
if (ev != null)
{
ev(this, new System.ComponentModel.PropertyChangedEventArgs("OrderedSongs"));
}
}
//set the UI to bind to the following list of the songs. ordered by name.
public IEnumerable<CompositionVM> OrderedSongs
{
get
{
return Songs.OrderBy(song => song.Name);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.