【发布时间】:2010-10-22 20:17:29
【问题描述】:
我有一个显示绑定到 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。:
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 文件并返回到 旧数据仍在显示的这个视图 。
我怎样才能告诉这个视图“刷新它的绑定”,以便它显示实际数据。
感觉就像我在这里用太多的 HTML/HTTP 隐喻来讨论 WPF,我觉得有一种更自然的方法可以让 ObservableCollection 自我更新,因此它的名字,但这是我能得到的唯一方法用户现在能够在 WPF 应用程序中编辑数据。因此,这里感谢任何级别的帮助。
【问题讨论】: