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

查看:368
本文介绍了如何获得的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()是一个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;
}

这一切工作正常,当用户进入到另一种观点认为EXCEPT,编辑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.

我怎么能讲这个观点为刷新其绑定,这样它显示的实际数据。

这感觉就像我在这里要对WPF太多的HTML / HTTP的比喻,我感觉到有一种更自然的方式来获得的ObservableCollection本身更新,因此它的名字,但是这是我能得到的唯一途径用户能在此刻WPF应用程序来编辑数据。因此,在任何级别的帮助就在这里pciated AP $ P $。

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.

添加和删​​除的名单将保持同步。当你想拿起外部修改(你可能有地方一个刷新按钮,或自然的地步,这是有道理的重新加载整个文件),你可以有你的视图模型提高的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 note: why do you give method names to properties?

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

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