Xaml中的动态列绑定 [英] Dynamic column binding in Xaml
问题描述
这是以前发布的后续问题(这里)。
This is a follow up question from an earlier post (here).
我有一些标题信息存储为:
字典< string,string>
- 第一个字符串表示字段名称,第二个字符串表示要显示的标题。
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.
我有一组动态数据存储为:
字典< string,object>
- 其中string是字段名。
I have a set of dynamic data that is stored as:
Dictionary<string, object>
- where string is the field name.
我绑定到这在xaml中为:
I bind to this in xaml as:
<data:DataGrid Name="_dataGrid" AutoGenerateColumns="True" IsReadOnly="False" Margin="5" Height="200">
<data:DataGrid.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>
问题1 我想自动生成这些列(使用提供的标题信息)
Problem 1 I want to autogenerate these columns (using the header information supplied)
问题2 我希望根据它们的数据类型生成列(即布尔=复选框)
Problem 2 I want the columns to be generated based on what datatype they are (i.e. boolean = checkbox)
问题3 理想情况下,我还要指定天气,一个按钮应该存在于第一列或不存在(即编辑/查看按钮)通过数据绑定
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()生成具有简单属性的类型。请参阅此帖关于如何生成这样的类型的细节
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
这篇关于Xaml中的动态列绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!