How to get IndexOf Object in .Net ItemCollection

619 Views Asked by At

From my ViewModel, I need to programmatically move the focus and highlight of a row in a WPF DataGrid. The DataGrid has just one column:

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

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

And in the datacontext ViewModel:

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

This works even though I don't have a property per se in the ViewModel for the data field "descr", because I bind the DataGrid's ItemSource.

In the ViewModel I can access the View DataGrid's ItemCollection of items by passing in that ItemCollection from the View like so:

<!-- 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>

And back in the ViewModel, I load the DataGrid items like so:

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

I want to keep the cast to ItemCollection so that I can retain the DataGrid properties of that ItemCollection. The problem is the ItemCollection's IndexOf method is not working. I only get -1 when I try to find the index of one of the class object items by doing this.

  var idx = _dgItems.IndexOf(myobject);

EDIT ------- this is entire code of the method 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();}

EDIT --------------------------------- This is the easier approach but I still need help with the lambda / filter expression and method call

// 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();}

END EDIT ---------------------

In debugger I can see the class Objects in _dgItems. If I do this, it works.

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

But the IndexOf method does not work when the parameter is just a class Object. I think the problem is with my cast of the DataGrid items to an ItemCollection. I need to cast the class Object, ie. myobject, to something recognizable by the ItemCollection that I got from the DataGrid. Is there a workaround? Thank you.

1

There are 1 best solutions below

8
ViVi On BEST ANSWER

Try this.

You need to cast it to the type of collection ie AdrTypeMdl. You cannot simply get the index by passing an object. You're binding to a source ItemsLcv which is of type AdrTypeMd1. So pass that exact type to get the exact index.

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

idx will be the index of that corresponding column.