如何获取 ItemsSource 以刷新其绑定? [英] How to get an ItemsSource to refresh its bind?

查看:23
本文介绍了如何获取 ItemsSource 以刷新其绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图显示绑定到 GetAll() 的列表框:

I've got a view which shows a listbox that is bound to GetAll():

<DockPanel>
    <ListBox ItemsSource="{Binding GetAll}"
             ItemTemplate="{StaticResource allCustomersDataTemplate}"
             Style="{StaticResource allCustomersListBox}">
    </ListBox>
</DockPanel>

GetAll() 是我的 ViewModel 中的 ObservableCollection 属性:

public ObservableCollection<Customer> GetAll
{
    get
    {
        return Customer.GetAll();
    }
}

依次调用 GetAll() 模型方法,该方法读取 XML 文件以填充 ObservableCollection.:

which in turn calls a GetAll() model method which reads an XML file to fill the ObservableCollection.:

public static ObservableCollection<Customer> GetAll()
{
    ObservableCollection<Customer> customers = new ObservableCollection<Customer>();

    XDocument xmlDoc = XDocument.Load(Customer.GetXmlFilePathAndFileName());
    var customerObjects = from customer in xmlDoc.Descendants("customer")
                          select new Customer
                          {
                              Id = (int)customer.Element("id"),
                              FirstName = customer.Element("firstName").Value,
                              LastName = customer.Element("lastName").Value,
                              Age = (int)customer.Element("age")
                          };
    foreach (var customerObject in customerObjects)
    {
        Customer customer = new Customer();

        customer.Id = customerObject.Id;
        customer.FirstName = customerObject.FirstName;
        customer.LastName = customerObject.LastName;
        customer.Age = customerObject.Age;

        customers.Add(customer);
    }

    return customers;
}

这一切都很好,除非用户转到另一个视图,编辑 XML 文件并返回到这个视图,旧数据仍在显示.

This all works fine EXCEPT when the user goes to another view, edits the XML file and comes back to this view where the old data is still showing.

我如何告诉这个视图刷新其绑定"以显示实际数据.

感觉就像我在这里用太多的 HTML/HTTP 比喻来讨论 WPF,我觉得有一种更自然的方法可以让 ObservableCollection 自我更新,因此它的名字,但这是我能得到的唯一方法用户现在能够在 WPF 应用程序中编辑数据.因此,在此感谢任何级别的帮助.

It feels like I am going about WPF here with too much of an HTML/HTTP metaphor, I sense there is a more natural way to get ObservableCollection to update itself, hence its name, but this is the only way I can get the user to be able to edit data in a WPF application at the moment. So help on any level is appreciated here.

推荐答案

ItemsControl 请求其绑定一次,然后缓存引用.

An ItemsControl requests its binding once and caches the reference thereafter.

如果集合对象的内容被修改,并且它实现了 INotifyCollectionChanged(就像 ObservableCollection 那样),它将拾取任何添加或删除的对象.

If the content of the collection object are modified, and it implements INotifyCollectionChanged (as ObservableCollection does), it will pick up any added or removed object.

现在,如果您希望绑定为 ListBox 提供一个新的集合对象,您可以让您的视图模型实现 INotifyPropertyChanged 并引发 PropertyChanged,传入 GetAll 作为属性名称.这将具有警告绑定属性值已更改的效果(有一个新的 ObservableCollection 准备好被拾取),它将提供给 ListBox,这将重新生成它的项目.

Now, if you want the binding to supply a new collection object to the ListBox, you can have your view-model implement INotifyPropertyChanged and raise PropertyChanged, passing in GetAll as the property name. This will have the effect of warning the binding that the property value has changed (there is a new ObservableCollection ready to be picked up), which it will supply to the ListBox, which will re-generate its items.

因此,只要您影响应用程序的更改,处理 GetAll 返回的 ObservableCollection,您就可以添加和删除,并且列表将保持同步.当您想要进行外部修改时(您可能在某处有一个刷新按钮,或者可以重新加载整个文件的自然点),您可以让您的视图模型引发 PropertyChanged 事件,它会自动调用属性getter,它会调用静态方法,它会返回一个全新的集合.

So as long as you effect changes from your app, working on the ObservableCollection returned by GetAll, you can add and remove and the list will stay in synch. When you want to pick up external modifications (you might have a refresh button somewhere, or a natural point where it makes sense to reload the whole file), you can have your view-model raise the PropertyChanged event, which will automatically call the property getter, which will call the static method, which will return a fresh new collection.

Nitpicker 注释:为什么要为属性指定方法名称?

Nitpicker note: why do you give method names to properties?

这篇关于如何获取 ItemsSource 以刷新其绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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