0

我查看了动态对象的文档,但我无法找到解决当前问题的方法。

我有这个代码:

dynamic sampleObject = new ExpandoObject();

string columnName = checkBox.Content.ToString();

sampleObject.columName = myObservableCollection;

现在,我添加的属性的名称将是 columnName,但我希望它是我的复选框的内容。

我应该如何实施?

谢谢

4

1 回答 1

0

使用命名值字典而不是动态属性。

您可以使用字典转换器绑定到字典中的特定条目(使用转换器参数值作为键)。来自此链接的以下代码

public class DictionaryItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dict = value as Dictionary<string, string>;
        if (dict != null)
        {
            return dict[parameter as string];
        }
        throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在通过代码向网格添加列的地方,您需要使用正确的转换器和名称转换器参数创建绑定。

于 2012-05-14T14:31:23.760 回答