6

所以基础知识:我有一个带有 ListView 的窗口,它由我的网格的数据上下文填充:

mainGrid.SetBinding(Grid.DataContextProperty, 
    new Binding() { 
        Source = new DataView() 
            { Table = SQLHandler.GetHandler[classType.ToString()] } 
    }
);

在xml中:

<ListView Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding}">

一切正常,它的人口。正如您在上面看到的,我有一个可以由 Singleton 访问的 SQLHandler 类,并且我可以使用索引器访问我的表。

问题:窗口加载,我正在选择一行,单击编辑按钮,新窗口加载,我在其中获取所选行的详细信息。当我通过这个新窗口删除这一行并关闭它时,主窗口(显示完整数据表的位置)不会相应更新。我知道解决方案应该是什么,但我无法让它发挥作用。(inotifyproperty 将接口更改为 SqlHandler 类、Binding.IndexerName 等。)

这是主要的事情:数据集不在我的 SqlHandler 类中,它在 SqlExecuter 中,我的所有 sqlcommands 都在其中执行。

public override DataTable this[string key]
{
    get
    {
        if (sqlExecuter.GetDataSet.Tables.Contains(key)) 
            return sqlExecuter.GetDataSet.Tables[key];
        throw new KeyNotFoundException("The specified key was not found");
    }
}

其中 GetDataSet 是:

public DataSet GetDataSet
{
    get { return ds; }
}

我怎样才能使这项工作?当我在另一个窗口中删除一行并关闭该行时,主窗口的列表视图不会自行更新。我唯一的选择是放一个刷新按钮,然后重新绑定 datacontext 属性,然后它当然可以工作,但我的目标是拥有一个“实时”更新系统,毕竟这就是 Binding 的用途。

我尝试过: SqlExecuter 中的 GetDataSet:实现了 inotifypropertychanged 接口,但没有任何改变。而且我不能在 SqlHandler 的索引器上实现 inotifypropertychanged,因为它没有设置器,我总是只是从代码隐藏访问表,我的 sqldataadapter 正在填充它们(填充方法)

ps:我真的不打算创建一个 ObservableCollection,因为我 90% 的代码应该被重写,当我删除一行时,我会清除我的数据集并再次填充它,所以我什至不希望它注意到每一个改变,就在我重新填充我的数据表时,我的列表视图应该知道它..并刷新自己

4

2 回答 2

1

I think using a second window as a popup might be the problem here. If you were doing the editing on the same page then you could use a simple

ListView1.DataBind()

To refresh the contents of the list at the end of the delete command. Or you could use the IsPostBack method to update the list if the page was bring redrawn rather than reloaded.

You could try calling the page name and then the list view from your other window but I'm not sure if you can perform that kind of command from another window.

Alternatively you could do the editing on a different page, rather than a separate window so when you return to the original page the listview is being redrawn.

Like yourself I'm sure there is a simpler solution to your problem, but unfortunately I don't know it.

于 2013-06-18T09:00:02.963 回答
0

您可能需要将绑定模式设置为两种方式:

Mode = BindingMode.TwoWay

WPF 出于一些不明显的原因,默认为单向绑定。希望有帮助。我不是特别了解情况,但我已经看到了与 ObservableCollections 的简单数据绑定的问题,并且 TwoWay 绑定修复了它。

于 2012-12-28T06:04:18.173 回答