开发者

WPF databinding to a linked list

I want to databind a ListBox to a linked list

public class MyClass
{
    public string MyText{ get; set; }
    public MyClass PreviousItem{ get; set; }
}

I want to use an instance of MyClass as the datasource of a ListBox, to bas开发者_StackOverflowically show a list of the MyClass instance and all it's PreviousItems.

Of course binding to an instance of MyClass will result in only the topmost parent being shown. What would be the best approach for this?


Why do you need a custom implementation of a LinkedList in the first place? There is a .NET implementation already: System.Collections.Generic.LinkedList

Other than that, you have basically three options:

  1. Recommended: If it fits your business logic, impement at least IEnumerable in MyClass (like the .NET lists)
  2. Create a ViewModel which traverses your items of MyClass and puts them into an ObservableCollection
  3. Create an IValueConverter object to convert your linked list to a collectionType like ObservableCollection


You can use BCL LinkedList<T> class. Or alternatively if you like your class very much you can implement IEnumerable like this

public class MyClass : IEnumerable<MyClass>
    {
        public string MyText { get; set; }
        public MyClass PreviousItem { get; set; }

        public IEnumerator<MyClass> GetEnumerator()
        {
            var item = this;
            do
            {
                yield return item;
                item = item.PreviousItem;
            } while (item != null);

        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }


Actually implementing IEnumerable would suffice:

    public IEnumerable<MyClass> TraverseItemsFromCurrent
    {
        get
        {
            var current = this;
            yield return current;
            while (current.PreviousItem != null)
            {
                current = current.PreviousItem;
                yield return current;
            }
        }
    }

And yes, using LinkedList<T> might be easier and more adequate to your purpose.


The easiest thing (in my opinion) is to implement IEnumerable

public class MyClass : IEnumerable<MyClass>
{
    public string MyText { get; set; }
    public MyClass PreviousItem { get; set; }

    IEnumerator<MyClass> IEnumerable<MyClass>.GetEnumerator()
    {
        for (var item = this; item.PreviousItem != null; item = item.PreviousItem)
            yield return item;
    }

    public IEnumerator GetEnumerator()
    {
        return ((IEnumerable<MyClass>)this).GetEnumerator();
    }
}

Then you code would look like this

public Window1()
    {
        MyClass item1 = new MyClass() {MyText = "No1"};
        MyClass item2 = new MyClass() {MyText = "No2"};
        MyClass item3 = new MyClass() {MyText = "No3"};
        MyClass item4 = new MyClass() {MyText = "No4"};

        item4.PreviousItem = item3;
        item3.PreviousItem = item2;
        item2.PreviousItem = item1;

        DataContext = item4; // your first item
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜