【问题标题】:MoveCurrentTo DataGrid将当前移动到数据网格
【发布时间】:2017-02-03 19:22:09
【问题描述】:

加载某个位置的客户列表后,我想选择特定客户作为当前项目。当我尝试移动到当前项目时,没有选择任何项目。我已经(通过调试)验证了我想要移动到的项目存在。

如果我移动到当前项目

(CustomerList.MoveCurrentTo(CustomerList.CurrentItem) //it works but is senseless.
CustomerList is an ICollectionView.

我的问题是“如何将当前项目焦点移动到我选择的特定项目?”。

CustomerList = new CollectionView ( _service.GetCustomers().Where(l => l.LocationID == customer.LocationID));
var item = CustomerList.Cast<AlarmPermit.Entities.Customer>().Where(l => l.BRKey == 52151).Single();
CustomerList.MoveCurrentTo(item);

【问题讨论】:

  • 1) 在以var item = CustomerList.Cast... 开头的行上设置断点,看看你是否得到了你期望的项目。如果是,那么问题出在您的CustomerList.MoveCurrentTo(item) 中,未显示。 2) 每次运行此代码时,您都会创建 CustomerList 的新实例。您在 UI 中使用的任何控件都可能绑定到现有的“CustomerList”,而这不是您正在使用的。
  • @ElementalPete 我确实设置了一个断点,我得到了我期待的项目。我不确定您为什么看不到我的 CustomerList.MoveCurrentTo(item) 语句。它就在那里。此代码位于视图模型的构造函数中。所以这只执行一次。我有一个消息框显示 CurrentChanged 事件中的项目,以查看我得到了什么。我得到一个空项目作为当前项目。
  • 我可以看到你在哪里调用CustomerList.MoveCurrentTo(item),我只是看不到那个方法的作用。如果item不为null,而你将item传递给MoveCurrentTo方法,而MoveCurrentTo方法负责设置当前项,而当前项没有被设置,那么问题很可能在MoveCurrentTo 方法中...
  • MoveCurrentTo 不是我的方法。它是 ICollectionView 接口的一部分。
  • 哦,好的,明白了。在这种情况下,我的第二个猜测是你的电脑闹鬼了。

标签: c# wpf mvvm datagrid prism-6


【解决方案1】:

问题似乎是我使用了 CollectionView 而不是从该类派生的类。 (谢谢@R.Richards)。 MSDN 文档表明 CollectionView 不应直接使用。

在我将集合从 CollectionView 更改为 ListCollectionView 后,代码工作了。 CustomerList 仍然是 ICollectionView,但我将 ListCollectionView 对象分配给它。

这里是修改后的代码:

CustomerList = new ListCollectionView ( _service.GetCustomers().Where(l => l.LocationID == customer.LocationID).ToList());
var item = CustomerList.Cast<AlarmPermit.Entities.Customer>().Where(l => l.BRKey == 52151).Single();
CustomerList.MoveCurrentTo(item);

【讨论】:

  • 找驱魔人更简单! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 2017-09-09
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多