设置DataGrid

时间:2016-01-13 08:28:16

标签: c# silverlight datagrid

我正在后面的代码中生成一个超链接按钮。问题是列首先出现在数据网格中,尽管将属性设置为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);      
}

1 个答案:

答案 0 :(得分:0)

设置现有列的DisplayIndex是否会改变其位置,而不是添加新列?

<sdk:DataGridTemplateColumn x:Key="HyperlinkColumn" Header="Hyperlink">

和代码

private void ProductGrid_AutoGeneratingColumn(object sender,
    DataGridAutoGeneratingColumnEventArgs e)
{
    var column = (DataGridColumn) Resources["HyperlinkColumn"];
    column.DisplayIndex = 4;
}