【问题标题】:ListBox does not refresh ObservableCollectionListBox 不刷新 ObservableCollection
【发布时间】:2012-08-03 06:27:13
【问题描述】:

我已经对该问题进行了一些研究,但找不到解决方法。

问题来了:

我有一个自定义类 ListedNote 的 observableCollection。起初 ListBox 显示数据,但有些数据是异步加载的,不会更新。 (例如用户图片)

<ScrollViewer VerticalScrollBarVisibility="Auto">
        <ListBox ItemsSource="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" x:Name="Content" ItemTemplate="{StaticResource ListBoxCardFBLikeTemplate}"
                 HorizontalContentAlignment="Stretch">

        </ListBox>
    </ScrollViewer>

视图模型

....
private ObservableCollection<ListedNote> notes;

public ObservableCollection<ListedNote> Notes
        {
            get { return notes; }
            set { notes = value; RaisePropertyChanged(() => this.Notes); }
        }
private void LoadAttachmentsAsync(ListedNote note)
        {
            Async.Call(() => this.ServiceConnector.RetrieveAnnouncementAttachment(note.IdValue),
                mm =>
                {
                    if (mm != null)
                    {
                        if (mm.MultimediaType.IsPicture)
                            note.AttachedPicture = mm;
                        else
                            note.AttachedFile = mm;

                        note.AttachmentData = new List<byte>(mm.Data);

                        var index = this.Notes.IndexOf(note);
                        if (index >= 0)
                            this.Notes[index] = note;


                    }
                });
        }

有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml data-binding listbox


    【解决方案1】:

    ObservableCollection 只会在集合的内容发生更改时通知 UI:即添加或删除 ListedNote 时。如果集合中已存在的ListedNote 的属性发生更改,它不会通知 UI。

    您的 ListedNote 类需要实现 INotifyPropertyChanged 接口,以便 UI 知道某个属性已在单个注释上发生更改。

    【讨论】:

    • ObservableCollection 不会像在我的 viewModel 中那样通知覆盖吗?
    • ObservableCollection在您的集合内容发生更改时通知 - 即,当您添加或删除项目时。更改集合中项目的属性不会更改集合中的项目,因此 ObservableCollection 不会帮助您。您需要您的 ListedNote 类在 属性更改时通知 UI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2013-04-01
    相关资源
    最近更新 更多