将Observable Collection绑定到ListView

时间:2013-01-16 21:20:09

标签: c# listview gridview binding observablecollection

XAML:

<Window x:Class="Genesis.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="725" Width="918" Loaded="Window_Loaded"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <ListView Margin="22,39,0,0" Name="Library" DataContext="{Binding}" ItemsSource="{Binding _songData}" HorizontalAlignment="Left" Width="854" Height="427" VerticalAlignment="Top">
        <ListView.View>
            <GridView x:Name="gvLibrary" >
                <GridViewColumn Width="220" Header="Title" DisplayMemberBinding="{Binding Title}" x:Name="gvColumnTitle" />
                <GridViewColumn Width="180" Header="Artist" DisplayMemberBinding="{Binding Artist}" x:Name="gvColumnArtist" />
                <GridViewColumn Width="180" Header="Album" DisplayMemberBinding="{Binding Album}" x:Name="gvColumnAlbum" />
                <GridViewColumn Width="180" Header="Location" DisplayMemberBinding="{Binding Location}" x:Name="gvColumnLocation" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

C#:

ObservableCollection<songInfo> songData = new ObservableCollection<songInfo>();

    public ObservableCollection<songInfo> _songData
    { 
        get 
        { 
            return songData; 
        } 
    }
public ObservableCollection<songInfo> getStoredData()
    {

        string[] songs = System.IO.File.ReadAllLines("library");

        ObservableCollection<songInfo> songs_formatted = new ObservableCollection<songInfo>();

        foreach (string song in songs)
        {
            string[] data = song.Split('|');
            songs_formatted.Add(new songInfo
            {
                Title = data[0],
                Artist = data[1],
                Album = data[2],
                Location = data[3]
            });
        }
        return songs_formatted;


    }
private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (System.IO.File.Exists("library"))
        {
            songData = getStoredData();
        }
        else
        {
            F.MessageBox.Show("Could not get your Library.");
        }

        F.MessageBox.Show(songData.ToArray().Length.ToString());
    }
public class songInfo
    {
        public string Title { get; set; }
        public string Artist { get; set; }
        public string Album { get; set; }
        public string Location { get; set; }
    }

ObservableCollection正在运行时正确填充,并且所有元素都已正确格式化:问题与将集合绑定到列表视图和列有关。

1 个答案:

答案 0 :(得分:1)

问题是您正在设置songData但ListView绑定到_songData ListView不会知道您已更改songData

尝试将设置符添加到_songData并将getStoredData();设置为该值。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (System.IO.File.Exists("library"))
        {
            _songData = getStoredData();
        }
    }

    private ObservableCollection<songInfo> songData = new ObservableCollection<songInfo>();
    public ObservableCollection<songInfo> _songData
    {
        get { return songData; }
        set { songData = value; }
    }