views:

124

answers:

3

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?

+2  A: 

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
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
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
+1  A: 

See this question

John Bowen
This works. You need to bind to the DataGridRow AlternationIndex.
mdm20
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
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
You're correct. Unfortunately, even after setting the AlternationCount to a substantial high number, AlternationIndex appears to be 0 for all items.
jaws
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
+1  A: 

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