0

I've got a list of items that I'm placing in an ObservableCollection which I am then databinding to a datagrid UI created in XAML. One of the columns displayed is the index of the item in the list. Currently, I am creating a property and binding to it. However, this creates a lot of work when I need to update/add/remove items from the list--I have to go through all affected items and change their property. My question is: Is there a way to automatically display the index of an item in the collection in the UI?

jaws
  • 1,952
  • 4
  • 20
  • 27
  • [See this question](http://stackoverflow.com/questions/3451422/wpf-bind-to-item-index-from-within-itemtemplate-of-itemscontrol/) – John Bowen Sep 01 '10 at 19:16
  • This works. You need to bind to the DataGridRow AlternationIndex. – mdm20 Sep 01 '10 at 19:33
  • This looks like precisely what I need. However, the specific control I am using is a RadTreeListView and I cannot seem to get at the AlternationIndex. Any ideas? – jaws Sep 02 '10 at 18:36
  • AlternationIndex is an inherited Attached Property owned by ItemsControl that can be accessed in XAML on any element with ItemsControl.AlternationIndex. AlternationCount is a property on ItemsControl that should show up on RadTreeListView (it should be derived from ItemsControl). – John Bowen Sep 02 '10 at 20:43
  • You're correct. Unfortunately, even after setting the AlternationCount to a substantial high number, AlternationIndex appears to be 0 for all items. – jaws Sep 03 '10 at 15:21
  • There might be something that the control itself is doing that's changing how the AlternationIndex is set. You could try using Snoop to see if you can find a parent element somewhere that's getting a changing index. – John Bowen Sep 03 '10 at 15:48

2 Answers2

2

You could use a Converter for your binding where your converter would perform a lookup to find the index. Then, no matter where your item is located at in the collection, you would have the correct index.

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • So I implemented this, but the Converter is not called automatically when an item is added/removed from the list. So for example, when I remove an item, all items below it retain their old index value. – jaws Sep 02 '10 at 18:36
  • sounds like its not updating on the collection changed event,sounds like you need to attach a handler and, inside it, force an update. – Muad'Dib Sep 02 '10 at 20:52
1

Have you considered implementing a viewmodel over the top? Then you could display your ObservableCollection however you want and implement it under the covers

public class IndexedObject
{

    private object _rootObject;
    private int _index;

    public IndexedObject(object rootObject, int index)
    {
        _rootObject = rootObject;
        _index=index;
    }

    public string Value
    {
        get
        {
            return _rootObject.ToString();
        }
    }

    public int Index
    {
        get
        {
            return _index;
        }
    }
}

Then you can implement this property on the class or on the class that displays your ObservableCollection

    ObservableCollection<object> _yourCollection;

    public IEnumerable<IndexedObject> IndexedObjects
    {

        get
        {
            for (int i = 0; i < _yourCollection.Count; i++)
            {
                yield return new IndexedObject(_yourCollection[i], i);
            }
        }
    }

Then you would make the ObservableCollection collectionchanged event notify the program that your IndexedObjects property has changed. Then you can just bind to the IndexedObjects property and use the Index property irrespective of what the actual collection underneath looks like :)

TerrorAustralis
  • 2,833
  • 6
  • 32
  • 46