正在设置列在DataGrid中的显示位置
本文关键字:显示 位置 DataGrid 设置 | 更新日期: 2023-09-27 18:30:02
我正在代码后面生成一个超链接按钮。问题是,尽管将属性设置为DisplayIndex=4
,但该列会首先出现在数据网格中。该列会先出现,然后是其余列。如何修改此列的顺序?
XAML代码
<sdk:DataGrid AutoGenerateColumns="True" HorizontalAlignment="Left" Height="163" VerticalAlignment="Top" Width="380" Margin="10,59,0,0" Grid.Row="2" Grid.Column="1" Name="ProductGrid" Visibility="Collapsed">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Hyperlink">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="gridTemplate">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" >
<HyperlinkButton Content="Hyperlinkbutton" Tag="Hyperlinkbutton" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
代码隐藏
private void ProductGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.DisplayIndex = 4;
templateColumn.CellTemplate = (DataTemplate)Resources["gridTemplate"];
ProductGrid.Columns.Add(templateColumn);
}
设置现有列的DisplayIndex
是否会更改其位置,而不是添加新列?
<sdk:DataGridTemplateColumn x:Key="HyperlinkColumn" Header="Hyperlink">
并编码
private void ProductGrid_AutoGeneratingColumn(object sender,
DataGridAutoGeneratingColumnEventArgs e)
{
var column = (DataGridColumn) Resources["HyperlinkColumn"];
column.DisplayIndex = 4;
}