是否可以更改DataGrid列的列是动态的?

时间:2015-08-13 23:24:21

标签: wpf

我需要创建dataGrid,他的每个列都包含textBox和comboBox 但我的dataGrid列是在应用程序启动时创建的 - 通过读取一些配置文件...应用程序知道要创建多少列以及每列的标题名称。

我甚至不知道如何创建每个动态包含TextBox和ComboBox的可能性(我想以某种方式从xaml>

开始)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您需要使用DataGridTemplateColumn并按顺序绑定TextBox文本和ComboBox项目源属性以使用自定义模板。

<DataGrid ItemsSource="{Binding ContentsToTransfer}">
            <DataGridTemplateColumn Header="Category">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>                                
                            <TextBox Text="{Binding Category}"/>
                            <ComboBox ItemsSource="{Binding Categories}"></ComboBox> 
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid>

link可以帮助您更好地理解它。