开发者

Dynamic column binding in Xaml

This is a follow up question from an earlier post (here).

I have some 'header' information stored as: Dictionary<string,string> - where the first string represents the field name, and the second the heading I want displayed.

I have a set of dynamic data that is stored as: Dictionary<string, object> - where string is the field name.

I bind to this in xaml as:

<data:DataGrid Name="_dataGrid" AutoGenerateColumns="True"  IsReadOnly="False" Margin="5" Height="200">
            <data:DataGri开发者_运维技巧d.Columns>
                <data:DataGridTextColumn Header="Forename" CanUserSort="True" SortMemberPath="Forename" 
                                          Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Forename}"/>
                <data:DataGridTextColumn Header="Surname" CanUserSort="True" SortMemberPath="Surname" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Surname}"/>
                <data:DataGridTextColumn Header="Age" CanUserSort="True" SortMemberPath="Age" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Age}"/>
                <data:DataGridTextColumn Header="Shoesize" CanUserSort="True" SortMemberPath="Shoesize" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Shoesize}"/>                
            </data:DataGrid.Columns>
        </data:DataGrid> 

Problem 1 I want to autogenerate these columns (using the header information supplied)

Problem 2 I want the columns to be generated based on what datatype they are (i.e. boolean = checkbox)

Problem 3 Ideally I would also like to specify weather a button should exist in the first column or not (i.e. an edit / view button) via databinding


I've used an approach that follows the pattern of this pseudocode

columns = New DynamicTypeColumnList()
columns.Add(New DynamicTypeColumn("Name", GetType(String)))
dynamicType = DynamicTypeHelper.GetDynamicType(columns)

DynamicTypeHelper.GetDynamicType() generates a type with simple properties. See this post for the details on how to generate such a type

Then to actually use the type, do something like this

Dim rows as List(Of DynamicItem)
Dim row As DynamicItem = CType(Activator.CreateInstance(dynamicType), DynamicItem)
row("Name") = "Foo"
rows.Add(row)
dataGrid.DataContext = rows
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜