我如何解决这个lambda表达式外部变量问题?

Gro*_*ile 4 c# lambda wpfdatagrid icustomtypedescriptor

我正在使用PropertyDescriptor和ICustomTypeDescriptor(仍然)尝试将WPF DataGrid绑定到一个对象,数据存储在一个Dictionary中.

因为如果你向WPF DataGrid传递一个Dictionary对象列表,它将根据字典的公共属性(Comparer,Count,Keys和Values)自动生成列,我的Person子类为Dictionary,并实现ICustomTypeDescriptor.

ICustomTypeDescriptor定义了一个返回PropertyDescriptorCollection的GetProperties方法.

PropertyDescriptor是抽象的,所以你必须将它子类化,我想我有一个构造函数,它接受Func和一个Action参数,它们委托字典中值的获取和设置.

然后我为字典中的每个Key创建一个PersonPropertyDescriptor,如下所示:

            foreach (string s in this.Keys)
            {
                var descriptor = new PersonPropertyDescriptor(
                        s,
                        new Func<object>(() => { return this[s]; }),
                        new Action<object>(o => { this[s] = o; }));
                propList.Add(descriptor);
            }
Run Code Online (Sandbox Code Playgroud)

问题是每个属性得到它自己的Func和Action但它们都共享外部变量s所以尽管DataGrid自动生成"ID","FirstName","LastName","Age","Gender"的列,它们都得到了设置为"性别",这是foreach循环中s的最终静止值.

如何确保每个委托使用所需的字典密钥,即Func/Action实例化时的s值?

非常感谢.


这是我的其余想法,我只是在这里尝试这些不是'真正'的类......

// DataGrid binds to a People instance
public class People : List<Person>
{
    public People()
    {
        this.Add(new Person());
    }
}

public class Person : Dictionary<string, object>, ICustomTypeDescriptor
{
    private static PropertyDescriptorCollection descriptors;

    public Person()
    {
        this["ID"] = "201203";
        this["FirstName"] = "Bud";
        this["LastName"] = "Tree";
        this["Age"] = 99;
        this["Gender"] = "M";        
    }        

    //... other ICustomTypeDescriptor members...

    public PropertyDescriptorCollection GetProperties()
    {
        if (descriptors == null)
        {
            var propList = new List<PropertyDescriptor>();

            foreach (string s in this.Keys)
            {
                var descriptor = new PersonPropertyDescriptor(
                        s,
                        new Func<object>(() => { return this[s]; }),
                        new Action<object>(o => { this[s] = o; }));
                propList.Add(descriptor);
            }

            descriptors = new PropertyDescriptorCollection(propList.ToArray());
        }

        return descriptors;
    }

    //... other other ICustomTypeDescriptor members...

}

public class PersonPropertyDescriptor : PropertyDescriptor
{
    private Func<object> getFunc;
    private Action<object> setAction;

    public PersonPropertyDescriptor(string name, Func<object> getFunc, Action<object> setAction)
        : base(name, null)
    {
        this.getFunc = getFunc;
        this.setAction = setAction;
    }

    // other ... PropertyDescriptor members...

    public override object GetValue(object component)
    {
        return getFunc();
    }

    public override void SetValue(object component, object value)
    {
        setAction(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 7

只是:

        foreach (string s in this.Keys)
        {
            string copy = s;
            var descriptor = new PersonPropertyDescriptor(
                    copy,
                    new Func<object>(() => { return this[copy]; }),
                    new Action<object>(o => { this[copy] = o; }));
            propList.Add(descriptor);
        }
Run Code Online (Sandbox Code Playgroud)

对于捕获的变量,声明它是重要的.因此,通过在循环内声明捕获的变量,每次迭代都会得到一个不同的捕获类实例(循环变量s,在技术上声明在循环之外).