我正在使用 C# WPF MVVM。所以在 XAML 中有一个列表视图,它绑定到一个对象,用于根据选项卡显示来自 sql 数据库的不同信息。
例如。我有两种形式:一种是显示信息,另一种是用于输入信息。在另一种形式中输入新信息后,如何自动更新一种形式的列表视图?因为现在我必须切换标签才能更新列表视图。
After you enter new information into a form, try to invoke your own method, which will update your information into a list view.
So you can use some event eg. DataContentChanged
or your update method can be called when u click the button which adds new data into your form.
Example of refresh method should look like this:
public void 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;
}
EDIT: To finish and solve your problem, try to use this snippet of code:
//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();
}
After that your Graph1.xaml.cs should look like this:
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 ;-)
}
这个元素的绑定方向应该暴露给 TwoWay (Mode=TwoWay)
像这样:
x:Name="list" ItemsSource="{Binding ....... , Path=........., Mode=TwoWay}}" ......
除了默认绑定(一种方式)之外,您还可以将绑定配置为两种方式,一种方式到来源,等等。这是通过指定 Mode 属性来完成的。
OneWay:导致对源属性的更改自动更新目标属性,但源未更改 TwoWay:源或目标中的更改自动导致对另一个更新 OneWayToSource:导致对目标属性的更改自动更新源属性目标未更改 OneTime:仅导致对源属性的第一次更改会自动更新目标属性,但源不会更改,后续更改不会影响目标属性
你可以看看这个:http: //msdn.microsoft.com/en-us/library/ms752347.aspx