更新势必观察的集合中的一个要素 [英] Updating one element of a bound Observable collection

查看:71
本文介绍了更新势必观察的集合中的一个要素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 songInfo 调用自定义类中可观察到的集合。结果
它绑定到的ListView 。这里是code的绑定:

I have an observable collection with a custom class called songInfo inside.
It is bound to a ListView. Here is the code for the binding:

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在我的code。在其他点填充。当元素被添加或删除,在ListView更新。然而,还有点时,我简单地改变只是 songInfo.Ext songInfo.Location ,等我发现了一个令人费解办法做到这一点和更新,但我不得不删除元素,并重新添加它:

The songInfo is populated at other points in my code. When elements are added or removed, the ListView is updated. However, there are points when I simply change just songInfo.Ext or songInfo.Location, etc. I found a convoluted way to do this and update, but I had to remove the element and re-add it:

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。

That changes isSongPlaying to true.

有只更新一个GridView的?

推荐答案

songInfo 类需要实现的 INotifyPropertyChanged的

Your songInfo class needs to implement INotifyPropertyChanged.

如果你正确地实现了这个接口,然后通过更改为类的成员将自动获得用户界面中反映的结合。

If you implement this interface properly, then changes to members of the class will automatically get reflected within the user interface via the binding.

这篇关于更新势必观察的集合中的一个要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆