binding to a list of tuples

不羁岁月 提交于 2019-12-23 08:16:08

问题


I have a list of tuples pairing two pieces of data... I'd like to bind the list to a data grid. For display, it works fine... but if I try and modify an entry, it says "A TwoWay or OneWayToSource binding cannot work on the read-only property 'Item1'"... presumably Tuples are immutable in .NET 4.0. Is there an easy way to bind to pairs of data without creating a mutable tuple class of my own?


回答1:


Yes, tuples are immutable. Anonymous types are also immutable. You should use your own generic type:

public class Pair<T, U> 
{
     public Pair() {
     }

     public Pair(T first, U second) {
       this.First = first;
       this.Second = second;
     }

     public T First { get; set; }
     public U Second { get; set; }
};


来源:https://stackoverflow.com/questions/4017714/binding-to-a-list-of-tuples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!