创建动态嵌套GridView

时间:2013-07-25 09:57:20

标签: c# wpf

我有以下类结构 -

public class Details
{
    public string Customer { get; set; }
    public string Serial { get; set; }
    public string Model { get; set; }
}

public class Summary
{
    public string Header { get; set; }
    public List<Details> Details { get; set; }
}

我正在关注this链接以构建此要求。

我的要求就像拥有一个GridView的动态no列,这些列将由Summary类的List映射。现在Header将与GridViewColumn标头一起映射。直到现在还可以。现在从Details属性,我想再次创建一个动态号为GridView的子项。使用Details类的List和Details类属性中定义的行映射的列。任何人都可以告诉我正确的解决方案。

修改更新了链接

我尝试了什么

public class DynamicGridViewConverter: IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var dynamicConfig = value as List<Summary>;

        if (dynamicConfig != null)
        {
            var gridView = new GridView();
            foreach (var lineColumn in dynamicConfig)
            { 
                DataTemplate template = new DataTemplate();
                FrameworkElementFactory frameworkGrid = new FrameworkElementFactory(typeof(Grid));
                FrameworkElementFactory frameworkListView = new FrameworkElementFactory(typeof(ListView));

                GridView internalGridView = new GridView();

                GridViewColumn col1 = new GridViewColumn();
                col1.DisplayMemberBinding = new Binding("Customer");
                col1.Header = "Customer Header";
                internalGridView.Columns.Add(col1);

                GridViewColumn col2 = new GridViewColumn();
                col2.DisplayMemberBinding = new Binding("Serial");
                col2.Header = "Serial Header";
                internalGridView.Columns.Add(col2);

                GridViewColumn col3 = new GridViewColumn();
                col3.DisplayMemberBinding = new Binding("Model");
                col3.Header = "Model Header";
                internalGridView.Columns.Add(col3);

                frameworkListView.SetValue(ListView.ViewProperty, internalGridView);

                var itemSourceBinding = new Binding();
                itemSourceBinding.Source = lineColumn.Details;
                frameworkListView.SetBinding(ListView.ItemsSourceProperty, itemSourceBinding);

                frameworkGrid.AppendChild(frameworkListView);

                template.VisualTree = frameworkGrid;                    gridView.Columns.Add(new GridViewColumn() { Header = lineColumn.Header, CellTemplate = template, Width=200});
            }
            return gridView;
        }
        return Binding.DoNothing;
    }

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

    #endregion
}

显示View Can not be shared by more than one ListView

我缺少哪一部分?

0 个答案:

没有答案