Binding a Dictionary to a WinRT ListBox

前端 未结 2 704
无人共我
无人共我 2020-12-10 18:26

I\'ve read a number of posts on binding Dictionary to WPF ListView and ListBox but I can\'t get equivalent code to work in WinRT.



        
2条回答
  •  一生所求
    2020-12-10 18:56

    I've come up with a workaround, that involves generating your own Key Value pairs dynamically.

    If you've specialized Dictionary, just add this:

        public IEnumerable> Collection
        {
            get {
                foreach (var v in this)
                {
                    MyPair p = new MyPair() { Key = v.Key, Value = v.Value };
                    yield return p;
                }
             }
        }
    

    and define your Pair type:

    public class MyPair
    {
        public K Key { get; set; }
        public V Value { get; set; }
    }
    

    Also, be careful that you create a new object each time. Some items walk across the object, and store the return, which can lead to everything looking like the last item, if you try to reuse the MyPair like I originally did.

提交回复
热议问题