我正在使用C#WPF MVVM。所以在XAML中有一个列表视图,它被绑定到一个对象,用于根据选项卡显示来自sql数据库的不同信息。如何在向数据库中添加新项目后自动更新列表视图
例如。我有两种形式:一种是显示信息,另一种是用于输入信息。在以另一种形式输入新信息之后,如何以一种形式自动更新列表视图?因为现在我必须切换选项卡才能更新列表视图。此元素
我正在使用C#WPF MVVM。所以在XAML中有一个列表视图,它被绑定到一个对象,用于根据选项卡显示来自sql数据库的不同信息。如何在向数据库中添加新项目后自动更新列表视图
例如。我有两种形式:一种是显示信息,另一种是用于输入信息。在以另一种形式输入新信息之后,如何以一种形式自动更新列表视图?因为现在我必须切换选项卡才能更新列表视图。此元素
装订方向应该暴露在双向(模式=双向)
这样的:
X:名称= “列表” 的ItemsSource =“{结合.. .....,路径= .........,模式=双向}}” ......
除了默认绑定之外,您还可以将绑定配置为双向,单向源等等。这是通过指定Mode属性完成的。
单向:使更改源属性自动更新目标属性,但源没有得到改变 双向:更改源或自动目标造成更新到其他 OneWayToSource:原因更改目标属性以自动更新源属性,但目标不会更改 一次:仅导致首次更改源属性以自动更新目标属性,但源不会更改并且后续更改会执行不影响目标亲perty
你可以看看这个:http://msdn.microsoft.com/en-us/library/ms752347.aspx
在我看来,问题是我应该更新绑定到ListView对象莫名其妙。 – user1669713 2013-03-07 08:21:12
看起来相同的例子,他们会帮助你http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part-1 – Zhenia 2013-03-07 08:24:22
我有Listview绑定到一个对象,其中包含所有信息。对象使用NHibernate从SQL数据库获取信息。但在将新信息添加到数据库后,它不会更新。所以绑定不会帮助我。在我看来... – user1669713 2013-03-07 08:30:44
您输入新信息的表单后,尝试调用自己的方法,这将更新您的信息的列表视图。所以你可以使用一些事件,例如。 DataContentChanged
或者当你点击将新数据添加到表单中的按钮时,可以调用你的更新方法。刷新方法的 例子应该是这样的:
公共无效lbRefresh()
{
//create itemsList for listbox
ArrayList itemsList = new ArrayList();
//count how many information you wana to add
//here I count how many columns I have in dataGrid1
int count = dataGrid1.Columns.Count;
//for cycle to add my strings of columns headers into an itemsList
for (int i = 0; i < count; i++)
{
itemsList.Add(dataGrid1.Columns[i].Header.ToString());
}
//simply refresh my itemsList into my listBox1
listBox1.ItemsSource = itemsList;
}
< \码>
我希望它能帮助,对不起,小姐了解如果发生。
编辑:为了完成和解决您的问题,尝试使用此代码段:
//some btn_Click Event in one window //(lets say, its your callback " to update" button in datagrid) private void Button_Click_1(object sender, RoutedEventArgs e) { //here you doing somethin //after your datagrid got updated, try to store the object, //which u want to send into your eg. listbox data[0] = data; //my stored data in array //for better understanding, this method "Button_Click_1" is called from Window1.xaml.cs //and I want to pass information into my another window Graph1.xaml.cs //create "newWindow" object onto your another window and send "data" by constuctor var newWindow = new Graph1(data); //line * //you can call this if u want to show that window after changes applied newWindow.Show(); }
您Graph1.xaml后。CS应该是这样的:
public partial class Graph1 : Window {//this method takes over your data u sent by line * into previous method explained public Graph1(int[]data) { InitializeComponent(); //now you can direcly use your "data" or can call another method and pass your data into it ownListBoxUpdateMethod(data); } private void ownListBoxUpdateMethod(int[] data) { //update your listbox here and its done ;-) }
我希望它能帮助。 凯文天空。
非常感谢您的回应。其实它是最适合我的。问题是我没有直接访问程序代码中的listview,因为它是MVVM。所以我必须以某种方式更新对象。但我想我已经接近解决问题了。 – user1669713 2013-03-07 10:42:09
通过收听活动?有了这个模糊的问题(没有代码),你不会在这里找到很多帮助。 – Najzero 2013-03-07 08:24:49
我想通过收听活动...... – user1669713 2013-03-07 09:20:37