我有一些麻煩將我的集合綁定到我的列表視圖。我嘗試了很多不同的方法,來自其他人在這裏進行綁定,並且最初也遵循this tutorial。我做了它的工作,但它不是我想要的。將ObservableCollection綁定到ListView
Anymway,這裏是XML:
<ListView Grid.Row="0" ItemsSource="{Binding SongList}" SelectionMode="Extended" x:Name="ListViewMain" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="0,1,0,0" Height="264" >
<ListView.View>
<GridView>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" Width="500"/>
<GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}" Width="100"/>
<GridViewColumn Header="Album" DisplayMemberBinding="{Binding Album}" Width="100"/>
<GridViewColumn Header="Length" DisplayMemberBinding="{Binding Length}" Width="100"/>
<GridViewColumn Header="Location" DisplayMemberBinding="{Binding Songfile}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
在我的代碼,將項添加到我的收藏,我做的:
public MainWindow()
{
InitializeComponent();
...
PlayListItem addsong = new PlayListItem(title, artist, album, length, filename);
}
的PlayListItem類用的ObservableCollection:
public class PlayListItem
{
public ObservableCollection<Song> _SongList = new ObservableCollection<Song>();
public ObservableCollection<Song> SongList { get { return _SongList; } }
public PlayListItem(string _Title, string _Artist, string _Album, string _Length, string _Filename)
{
_SongList.Add(new Song
{
Title = _Title,
Artist = _Artist,
Album = _Album,
Length = _Length,
SongFile = _Filename,
});
}
public class Song
{
public string Artist { get; set; }
public string Album { get; set; }
public String Title { get; set; }
public string Length { get; set; }
public String SongFile { get; set; }
}
}
我認爲每次我調用構造函數時都會正確添加項目,但不會更新在ListView上。我在輸出窗口中也沒有關於綁定的一些錯誤。
任何想法和幫助,將不勝感激。
編輯:
通過添加:
ListViewMain.ItemsSource = addsong.SongList;
右鍵創建一個新的PlayListItem似乎解決了問題,因爲ListView控件正在打印項目之後。
的可能重複的[WPF的ObservableCollection VS的BindingList (http://stackoverflow.com/questions/6254441/wpf-observablecollectiont-vs-bindinglistt) –
MethodMan
2013-03-25 18:22:42
後'的InitializeComponent右()','放的DataContext =此; '。 – Steve 2013-03-25 21:04:53
您必須從歌曲列表類中獲取歌曲列表集合。只要把它放在你的主窗口。您 – Entrodus 2015-10-08 15:09:03