RadGridView 动态列添加

RadGridView Dynamic columns adding

我正在尝试在我的 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;
    }

知道为什么名称和标记没有显示在单元格中吗?

单元格的 DataContext 是一个 EmployeeRecord 对象,因为您将 ItemsSource 属性 设置为 ICollection<EmployeeRecord>.

如果要在列中显示员工的所有主题,可以使用绑定到 EmployeeRecordSubjects 集合的 ItemsControl:

<DataTemplate x:Key="SubjectTemplate" DataType="ticket212220CreateGridColumnsOnTheFly:Subject">
    <ItemsControl ItemsSource="{Binding Subjects}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0 3 0 0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

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

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Mark" FontWeight="Bold" Margin="0 0 5 0"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Mark}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>