簡體   English   中英

將可觀察的集合綁定到ListView

[英]Binding an Observable Collection to a ListView

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在運行時被正確填充,並且所有元素的格式都正確:問題與將集合綁定到Listview和列有關。

問題是您正在設置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; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM