添加

时间:2017-03-30 13:56:56

标签: wpf telerik radgridview dynamic-columns

我试图在RadGridView中组合静态和动态列。 问题是动态列表示具有应在一个单元格内显示的不同属性的对象。

这就是我添加列的方式:

InitializeComponent();

        // Get some mock data
        ICollection <EmployeeRecord> employeeRecords = GetDummyData();

        this.grid.ItemsSource = employeeRecords;

        //Add the known columns
        this.grid.Columns.Add(new GridViewDataColumn() 
        { 
            UniqueName = "EmployeeName"
            , DataMemberBinding = new Binding("EmployeeName")
        });

        this.grid.Columns.Add(new GridViewDataColumn()
        {
            UniqueName = "ID"
            , DataMemberBinding = new Binding("ID")
        });


        // Now add the dynamic number of columns

        // Determines the maximum number of months that any employee has worked.
        int maxNumberOfMonths = employeeRecords.Max((x) => x.Subjects.Count);
        for (int i = 0; i < maxNumberOfMonths; i++)
        {
            this.grid.Columns.Add(new GridViewDataColumn()
            {
                UniqueName = "Subject " + (i + 1) ,
                DataMemberBinding = new Binding("Subjects[" + i + "]"),
                CellTemplate = this.Resources["SubjectTemplate"] as DataTemplate,
                DataType = typeof(Subject)
            });
        }

单元格的数据模板

    <DataTemplate x:Key="SubjectTemplate" DataType="ticket212220CreateGridColumnsOnTheFly:Subject">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Text="Subject"/>
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>

            <TextBlock Grid.Row="1" Grid.Column="0" Text="Mark"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Mark}"/>

        </Grid>
    </DataTemplate>

模型

 public class EmployeeRecord
{
    public string EmployeeName
    {
        get;
        set;
    }

    public int ID
    {
        get;
        set;
    }

    List<Subject> subjects = new List<Subject>();
    public IList<Subject> Subjects
    {
        get { return this.subjects; }
    }
}

public class Subject
{
    public string Name { get; set; }
    public int Mark { get;set; }
}

生成虚假数据

      static ICollection<EmployeeRecord> GetDummyData()
    {
        ICollection<EmployeeRecord> employeeRecords = new List<EmployeeRecord>();

        EmployeeRecord stevie = new EmployeeRecord() { EmployeeName = "Steven Gerrard", ID = 333 };
        stevie.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });

        employeeRecords.Add(stevie);

        EmployeeRecord ryan = new EmployeeRecord() { EmployeeName = "Ryan Giggs", ID = 222 };
        ryan.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });
        ryan.Subjects.Add(new Subject
        {
            Name = "Geograph",
            Mark = 3
        });
        employeeRecords.Add(ryan);

        EmployeeRecord john = new EmployeeRecord() { EmployeeName = "John Terry", ID = 111 };
        john.Subjects.Add(new Subject
        {
            Name = "Math",
            Mark = 5
        });
        john.Subjects.Add(new Subject
        {
            Name = "Geograph",
            Mark = 3
        });
        john.Subjects.Add(new Subject
        {
            Name = "Physics",
            Mark = 7
        });
        employeeRecords.Add(john);

        return employeeRecords;
    }

知道为什么名称和标记不会显示在单元格中?

1 个答案:

答案 0 :(得分:1)

单元格的DataContextEmployeeRecord对象,因为您将ItemsSource属性设置为ICollection<EmployeeRecord>

如果要在列中显示员工的所有主题,可以使用与ItemsControl的{​​{1}}集合绑定的Subjects

EmployeeRecord