【问题标题】:Updating one element of a bound Observable collection更新绑定的 Observable 集合的一个元素
【发布时间】:2013-01-10 01:18:34
【问题描述】:

我有一个可观察的集合,里面有一个名为 songInfo 的自定义类。
它绑定到ListView。这是绑定的代码:

C#

var songData = new ObservableCollection<songInfo>();

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

public class songInfo
{
    public string Title { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
    public string Location { get; set; }
    public string Ext { get; set; }
    public bool isSongPlaying { get; set; }
}

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="Genesis (Alpha)" Height="897" Width="882" Loaded="Window_Loaded"
    DataContext="{Binding RelativeSource={RelativeSource Self}}" Name="Genesis">

    <ListView Margin="12,39,0,0" Name="Library" DataContext="{Binding}" ItemsSource="{Binding _songData}" Height="681" VerticalAlignment="Top" MouseDoubleClick="Library_MouseDoubleClick"  ContextMenu="{StaticResource MyContextMenu}" AlternationCount="2" Background="AliceBlue" HorizontalAlignment="Left" Width="846">
        <ListView.View>
            <GridView x:Name="gvLibrary">
                <GridViewColumn Width="20" Header="hi" DisplayMemberBinding="{Binding isSongPlaying}" x:Name="gvColumnPlaying" />
                <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" />
                <GridViewColumn Width="80" Header="File Type" DisplayMemberBinding="{Binding Ext}" x:Name="gvColumnFileType" />
            </GridView>
        </ListView.View>
    </ListView>

songInfo 填充在我的代码中的其他点。添加或删除元素时,会更新 ListView。但是,有些地方我只是简单地更改 songInfo.ExtsongInfo.Location 等。我找到了一种复杂的方法来执行此操作并进行更新,但我不得不删除该元素并重新添加它:

songInfo temp = songData[playing_song_index];
songData.RemoveAt(playing_song_index);
songData.Insert(playing_song_index, new songInfo()
{
    Title = temp.Title,
    Artist = temp.Artist,
    Album = temp.Album,
    Location = temp.Location,
    Ext = temp.Ext,
    isSongPlaying = true
});

这会将 isSongPlaying 更改为 true。

有没有更简单的方法来更新 GridView 的一个“列”?

【问题讨论】:

    标签: c# listview gridview binding observablecollection


    【解决方案1】:

    您的songInfo 类需要实现INotifyPropertyChanged

    如果你正确实现了这个接口,那么对类成员的更改将通过绑定自动反映在用户界面中。

    【讨论】:

      【解决方案2】:

      如果 songInfo 不使用 INotifyPropertyChanged,那么您所做的将起作用。你可以通过以下方式清理一下:

      var mySong = songData.//get your song
      int index = songData.IndexOf( mySong );
      songData.Remove( mySong  );
      songData.Insert( index, mySong );
      

      【讨论】:

      • 我必须更改歌曲的属性。
      • 然后更新属性并重新插入。无需重新创建对象。但是,在您的 songData 类上使用 INotifyPropertyChanged 会更好。
      • 这个想法对我有用,虽然我的财产有 INotifyPropertyChanged。
      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2012-04-29
      相关资源
      最近更新 更多