【问题标题】:How to update listview automatically after adding a new item into DB将新项目添加到数据库后如何自动更新列表视图
【发布时间】:2013-03-07 08:06:32
【问题描述】:

我正在使用 C# WPF MVVM。所以在 XAML 中有一个列表视图,它绑定到一个对象,用于根据选项卡显示来自 sql 数据库的不同信息。

例如。我有两种形式:一种是显示信息,另一种是用于输入信息。在另一种形式中输入新信息后,如何自动更新一种形式的列表视图?因为现在我必须切换标签才能更新列表视图。

【问题讨论】:

  • 通过监听事件?对于这个模糊的问题(没有代码),您不会在这里找到太多帮助。
  • 我猜是通过列出事件...

标签: c# wpf listview


【解决方案1】:

这个元素的绑定方向应该暴露给 TwoWay (Mode=TwoWay)

像这样:

x:名称=“列表” ItemsSource="{Binding ....... , Path=........., Mode=TwoWay}}" ......

除了默认绑定,这是一种方式,您还可以将绑定配置为两种方式,一种方式来源,等等。这是通过指定 Mode 属性来完成的。

OneWay:导致对源属性的更改自动更新目标属性,但源不会更改 TwoWay:源或目标中的更改会自动导致另一个更新 OneWayToSource:导致对目标属性的更改自动更新源属性,但目标不会更改 OneTime:仅对源属性的第一次更改会自动更新目标属性,但源不会更改,后续更改不会影响目标属性

你可以看看这个:http://msdn.microsoft.com/en-us/library/ms752347.aspx

【讨论】:

  • 在我看来,问题是我应该以某种方式更新绑定到 listview 对象。
  • 看看同样的例子,他们会帮助你codeproject.com/Articles/29054/WPF-Data-Binding-Part-1
  • 我将 Listview 绑定到一个包含所有信息的对象。对象使用 NHibernate 从 SQL 数据库中获取信息。但是在将新信息添加到数据库后它不会更新。所以绑定对我没有帮助。在我看来……
【解决方案2】:

在表单中输入新信息后,尝试调用您自己的方法,该方法会将您的信息更新到列表视图中。 所以你可以使用一些事件,例如。 DataContentChanged 或者当您单击将新数据添加到表单中的按钮时,可以调用您的更新方法。 刷新方法示例应如下所示:

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;
}

编辑:要完成并解决您的问题,请尝试使用此 sn-p 代码:

//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。所以我必须以某种方式更新对象。但我想我已经接近解决问题了。
猜你喜欢
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
相关资源
最近更新 更多