我在 WPF 表单上有一个数据源,其形状是我的表的详细 View 。当我搜索特定行时,我希望它在绑定(bind)的文本框中更新,但它们只显示空白。
System.Windows.Data.CollectionViewSource employeesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeesViewSource")));
Yello.YelloDataSet yelloDataSet = ((Yello.YelloDataSet)(this.FindResource("yelloDataSet")));
var adapter = new YelloDataSetTableAdapters.EmployeesTableAdapter();
var row = yelloDataSet.Employees.Select("ID='11'");
employeesViewSource.View.MoveCurrentTo(row);
例如,如果我这样做:
System.Windows.Data.CollectionViewSource employeesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeesViewSource")));
employeesViewSource.View.MoveCurrentToNext();
它完美地工作并随着绑定(bind)更新。我是在使用错误的功能还是遗漏了什么??
最佳答案
我认为正在发生的事情是您从 yelloDataSet
中选择了一行然后你尝试移动到 employeesViewSource
中的那一行,但问题是行不一样,尽管数据可能是一样的。移动到您的 CollectionViewSource
中的一行,您必须在集合中搜索它。所以你会做这样的事情:
var row = employeesViewSource.View.OfType<DataSet>().Select("ID='11'");
employeesViewSource.View.MoveCurrentTo(row);
罢工><罢工>罢工>
编辑
好的,我没有实际尝试代码就回答了,我错了。尝试下一个代码,我认为它应该适合你:
var row = employeesViewSource.View.OfType<DataRowView>()
.Where(x => x.Row.Field<string>("ID") == "11")
.FirstOrDefault();
employeesViewSource.View.MoveCurrentTo(row);
但这取决于CollectionViewSource 是什么类型。我已经用数据表试过了。例如,如果它是一个数据集,它可能会改变。如果这不起作用,请告诉我源是什么类型...
关于c# - 将 View 更新为 WPF 中的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37520482/