【问题标题】:How to get IndexOf Object in .Net ItemCollection如何在 .Net ItemCollection 中获取 IndexOf 对象
【发布时间】:2016-08-20 06:48:07
【问题描述】:

从我的 ViewModel 中,我需要以编程方式移动 WPF DataGrid 中一行的焦点和突出显示。 DataGrid 只有一列:

   <DataGrid Name="DgAdrType" 
   ItemsSource="{Binding ItemsLcv}"
   IsSynchronizedWithCurrentItem="True" 

    <DataGridTextColumn Header="   Description" 
    IsReadOnly="True" 
    CanUserSort="True" Binding="{Binding descr, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

并且在数据上下文 ViewModel 中:

private IEnumerable<AdrTypeMdl> _itemsList;
ItemsLcv = CollectionViewSource.GetDefaultView(_itemsList) as ListCollectionView;

即使我在 ViewModel 中没有数据字段“descr”的属性,这仍然有效,因为我绑定了 DataGrid 的 ItemSource。

在 ViewModel 中,我可以通过从 View 中传入 ItemCollection 来访问 View DataGrid 的 ItemCollection,如下所示:

<!-- Interaction for click selection -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotMouseCapture">
<i:InvokeCommandAction Command="{Binding SelObjChangedCommand}"
  CommandParameter="{Binding ElementName=DgAdrType, Path=Items}"/>
  </i:EventTrigger>
  </i:Interaction.Triggers>

然后回到 ViewModel,我像这样加载 DataGrid 项目:

private ItemCollection _dgItems;
private void SelObjChanged(object theItems)
{if (theItems !=null)
{ _dgItems = theItems as ItemCollection;

我想保留对 ItemCollection 的强制转换,以便我可以保留该 ItemCollection 的 DataGrid 属性。问题是 ItemCollection 的 IndexOf 方法不起作用。当我尝试通过这样做来查找其中一个类对象项的索引时,我只得到-1。

  var idx = _dgItems.IndexOf(myobject);

EDIT ------- 这是方法 try IndesOf 的完整代码

private void HandleUpdateListEvent(Object myobject)
{AdrTypeMdl theNewItem = myobject as AdrTypeMdl;
bool co = _dgItems.Contains(theNewItem);
var idx = _dgItems.IndexOf(theNewItem);
_dgItems.MoveCurrentToPosition(idx);
_dgItems.Refresh();}

编辑--------------------------------- 这是更简单的方法,但我仍然需要 lambda / filter 表达式和方法调用方面的帮助

// this is where I try to get the index of an object for highlighting
private void HandleUpdateListEvent(Object myobject)
AdrTypeMdl theNewItem = myobject as AdrTypeMdl;
var e = ItemsLcv.SourceCollection.GetEnumerator();

ItemsLcv.Filter = o => (o == theNewItem);
foreach (row in ItemsLcv)
{  if row == theNewItem
return e >;
e = -1;}
ItemsLcv.MoveCurrentToPosition(e);
ItemsLcv.Refresh();}

结束编辑 ----------

在调试器中,我可以在 _dgItems 中看到类 Objects。如果我这样做,它会起作用。

        var idx = _dgItems.IndexOf(_dgItems[2]);

但是当参数只是一个类Object时IndexOf方法不起作用。我认为问题在于我将 DataGrid 项目转换为 ItemCollection。我需要转换类对象,即。 myobject,我从 DataGrid 获得的 ItemCollection 可识别的东西。有解决方法吗?谢谢。

【问题讨论】:

    标签: wpf datagrid indexof listcollectionview itemcollection


    【解决方案1】:

    试试这个。

    您需要将其转换为集合类型,即AdrTypeMdl。您不能简单地通过传递对象来获取索引。您正在绑定到 ItemsLcv 类型的源 AdrTypeMd1。所以传递那个确切的类型来得到确切的索引。

    var dgcolumn = myobject as AdrTypeMdl;
    if(dgcolumn != null)
    {
        var idx = _dgItems.IndexOf(dgcolumn);
    }
    

    idx 将是相应列的索引。

    【讨论】:

    • 是的,我试过了,甚至在下面做了一个明确的声明,但 idx 仍然是 -1。 AdrTypeMdl theNewItem = 有效载荷为 AdrTypeMdl。
    • 我的意思是整个代码,以便我可以尝试重现该问题。
    • 这是一个 Prism Unity 应用程序,需要很长时间才能缩小到这些工作部分。
    • 我认为的做法是在ItemsLcv中添加一个过滤方法。因为它是一个 CollectionView 并且双向绑定到 DataGrid 的 ItemCollection,所以我可以通过使用枚举器遍历 CollectionView 来获取我正在寻找的索引。然后我可以在 CollectionView 中更新 Currentitem,DataGrid 也会自动更新。我需要帮助来实现 lambda / filter 方法和方法调用。如何修改我添加到问题中的新代码?
    • 对不起。但我无法理解您上面发布的代码。试试foreach(var item in ItemsLcv) { //if some condition is met var idx = _dgItems.IndexOf(item); }
    猜你喜欢
    • 1970-01-01
    • 2014-05-31
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多