简体   繁体   English

ListView与DataTable绑定,删除行后不更新

[英]ListView Binding with DataTable, not updating after deleting row

So the basics: I've got a window with a ListView on it, which is populated by my grid's datacontext: 基础知识:我有一个带有ListView的窗口,它由我的网格的datacontext填充:

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

in xaml: 在xaml中:

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

everything works fine, it's populated. 一切正常,它已经填充。 As you can see above, i've got an SQLHandler class which can be accessed by Singleton, and I can access my tables with an indexer. 正如您在上面所看到的,我有一个可以被Singleton访问的SQLHandler类,我可以使用索引器访问我的表。

The problem: window loads up, i'm selecting a row, clicking the Edit button, new window loads up, where i get the selected row's details. 问题:窗口加载,我选择一行,单击编辑按钮,新窗口加载,我获取所选行的详细信息。 when i delete this row via this new window and close it, the main window (where the complete datatable is shown) is not updated accordingly. 当我通过这个新窗口删除此行并关闭它时,主窗口(显示完整数据表的位置)不会相应更新。 i know what the solution should be, but I can't make it work. 我知道解决方案应该是什么,但我不能让它发挥作用。 (inotifyproperty changed interface to SqlHandler class, Binding.IndexerName etc..) (inotifyproperty将接口更改为SqlHandler类,Binding.IndexerName等。)

here is the main thing: the dataset is not in my SqlHandler class, it's in SqlExecuter, where all of my sqlcommands are being executed. 这是主要的事情:数据集不在我的SqlHandler类中,它位于SqlExecuter中,我的所有sql命令都在其中执行。

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

where GetDataSet is: 其中GetDataSet是:

public DataSet GetDataSet
{
    get { return ds; }
}

How can I make this work? 我怎样才能做到这一点? When I delete a row in a different window and close that one, the mainwindow's listview doesn't update itself. 当我删除不同窗口中的一行并关闭该行时,主窗口的列表视图不会自行更新。 The only option I have is to put a refresh button up, and then rebind the datacontext property, then of course it's working, but my goal is to have a 'live' update system, that's what Binding is for after all. 我唯一的选择是刷新按钮,然后重新绑定datacontext属性,当然它正在工作,但我的目标是拥有一个“实时”更新系统,这毕竟是Binding的用途。

What I've tried: GetDataSet in SqlExecuter: implemented the inotifypropertychanged interface, but nothing changed. 我试过的:SqlExecuter中的GetDataSet:实现了inotifypropertychanged接口,但没有任何改变。 and i can't have inotifypropertychanged implemented on my indexer in SqlHandler, because it doesn't have a setter, I'm always just accessing the tables from code-behind, my sqldataadapter is populating them (Fill method) 我不能在SqlHandler中的索引器上实现inotifypropertychanged,因为它没有setter,我总是只是从代码隐藏中访问表,我的sqldataadapter填充它们(填充方法)

ps: i don't really plan on creating an ObservableCollection, because 90% of my code should be rewritten and when i delete a row, i clear my dataset and fill it up again, so I'm not even expecting it to notice every change, just when i refill my datatable, my listview should know about it.. and refresh itself ps:我真的不打算创建一个ObservableCollection,因为90%的代码都应该被重写,当我删除一行时,我清除我的数据集并重新填充它,所以我甚至不希望它注意到每一个改变,就在我重新填充我的数据表时,我的列表视图应该知道它...并刷新自己

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. 在delete命令末尾刷新列表的内容。 Or you could use the IsPostBack method to update the list if the page was bring redrawn rather than reloaded. 或者,如果页面重新绘制而不是重新加载,则可以使用IsPostBack方法更新列表。

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. 像你自己一样,我确信你的问题有一个更简单的解决方案,但不幸的是我不知道。

You may need to set the binding mode to two way: 您可能需要将绑定模式设置为两种方式:

Mode = BindingMode.TwoWay

WPF for some non-obvious reasons, defaults to one way binding. WPF出于一些非显而易见的原因,默认为单向绑定。 Hope that helps. 希望有所帮助。 I'm not particularly well-informed, but I've seen this issue with simple data bindings to ObservableCollections, and the TwoWay binding fixes it. 我并不是特别了解情况,但我已经看到了这个问题,简单的数据绑定到ObservableCollections,而TwoWay绑定修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM