WPF和EntityFramework中的两种方式绑定问题 [英] two way binding problem in wpf and entityframework

查看:82
本文介绍了WPF和EntityFramework中的两种方式绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我正在开发通过EntityFramework连接到sql服务器数据库的WPF应用程序.我的数据库中有一个名为Tasks的表.我的wpf应用程序中有一个Treeview控件,该控件绑定到EntityFramework模型中的Task实体. 首先,我使用此代码将TreeView绑定到Task实体.

Hi.
I am developing an WPF application that connect to a sql server database by EntityFramework . I have a table in my database named Tasks .I have a Treeview control in my wpf application that is bound to my Task entity in EntityFramework model .
First I bound TreeView to Task entity using this code .

MyEntity myEntity = new MyEntity();
myTreeView.ItemsSource = myEntity.Tasks;



然后,我使用以下代码添加了操作:



I Then did add operation using this code:

Task t = new Task();
t.ID = 10;
t.Title = "Task1";
myEntity.Tasks.Add(t);
myEntity.SaveChanges();



我注意到该代码更新了我的数据库,但是直到我重新启动应用程序后才自动更新UI.然后我用这段代码克服了这个问题:



I noticed that this code updated my database but did not update UI automatically until I restart my application . Then I used this code to overcome this problem :

IBindingList _bindingList;
_bindingList = ((IListSource)myEntity.Tasks).GetList() As IBindingList;
myTreeView.ItemsSource = _bindingList;



现在,当我添加任务时,它可以工作(它会更新UI):



Now When I add a Task it works (It updates UI):

t.ID = 10;
t.Title = "Task1"
_bindingList.Add(t);
myEntity.SaveChanges();



我的问题在这里.此应用程序已被我们局域网中的两台计算机上的两个人使用.当person1添加任务时,它不会更新person2应用程序中的UI.我曾在xaml中尝试过Mode = TwoWay,但这并没有改变任何东西.

每当计算机2在数据库中应用更改时,如何在计算机1中的应用程序UI中反映更改?

对不起,我的问题太长了,谢谢您的回答.



My problem is here . This application has been used by two persons on two computers in our local network . When person1 add a task it does not update UI in person2 application . I Have tried Mode = TwoWay in xaml but this did''nt change anything .

How can I reflect changes in my application UI in computer 1 whenever a change is applied in my Database by computer 2?

Sorry for my too long question and thanks for your answers.

推荐答案

您实现的INotifyPropertyChanged或INotifyCollection是否已适当更改.

当您将WPF对象绑定到CLR对象并且CLR对象发生更改并且您想要更新UI时,您必须正确实现INotifyproperty更改,因为常见的CLR属性不是依赖项属性.只有这样,两种方式的绑定才会起作用.
Are you implementing INotifyPropertyChanged or INotifyCollection changed appropriately.

When you bind a WPF object to a CLR object and the CLR object changes and you want to update the UI, you have to properly implement INotifyproperty changed because common CLR properties are not dependency properties. Only then a two way binding wil work.


这篇关于WPF和EntityFramework中的两种方式绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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