0

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运行时正确填充并且所有元素的格式都正确:问题与将集合绑定到列表视图和列有关。

4

1 回答 1

1

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

尝试添加一个 setter_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; }
    }
于 2013-01-16T21:38:48.287 回答