一个更好的方式来强制数据绑定WPF ListBox进行更新? [英] A better way of forcing data bound WPF ListBox to update?

查看:1091
本文介绍了一个更好的方式来强制数据绑定WPF ListBox进行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WPF ListBox绑定到一个ObservableCollection,
当收集更改,所有项目更新他们的位置。



新的位置存储在该集合,但UI不更新。
所以我添加了以下内容:

  void scenarioItems_CollectionChanged(object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ToolboxListItem.UpdatePositions();
lstScenario.ItemsSource = null;
lstScenario.ItemsSource = ToolboxListItem.ScenarioItems;
this.lstScenario.SelectedIndex = e.NewStartingIndex;
}

通过将ItemsSource设置为null,然后再次绑定,UI被更新



但这可能是非常糟糕的编码:p



建议?

解决方案

我有一个列表框绑定到一个对象属性,类型为 List< MyCustomType>()我确认下列代码在列表更新时更新列表框。

  void On_MyObjProperty_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e)
{
MyListBox.Items.Refresh();
}

如果仍然面临问题,请扫描VS IDE输出窗口(Ctrl + W,O),看看是否可以发现任何报告的绑定错误。


I have WPF ListBox which is bound to a ObservableCollection, when the collection changes, all items update their position.

The new position is stored in the collection but the UI does not update. So I added the following:

    void scenarioItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ToolboxListItem.UpdatePositions();
        lstScenario.ItemsSource = null;
        lstScenario.ItemsSource = ToolboxListItem.ScenarioItems;
        this.lstScenario.SelectedIndex = e.NewStartingIndex;
    }

By setting the ItemsSource to null and then binding it again, the UI is updated,

but this is probably very bad coding :p

Suggestions?

解决方案

I have a Listbox bound to an object property which is of type List<MyCustomType>() and I verified that the following code updates the listbox when the List is updated.

void On_MyObjProperty_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
   MyListBox.Items.Refresh();
}

If you're still facing issues, scan the VS IDE output window (Ctrl+W, O) and see if you can spot any binding errors reported.

这篇关于一个更好的方式来强制数据绑定WPF ListBox进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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