silverlight 4中datagrid列标题和列内容居中问题,增加自增长列

转载:http://www.cnblogs.com/guoyuanwei/archive/2011/01/02/1924163.html

命名空间:xmlns:Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"

再通过定义统一资源样式

在 <UserControl.Resources> 内定义下面的样式

<Style x:Key="DataGridHeaderStyle" TargetType="Primitives:DataGridColumnHeader">
     <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="DataGridCellStyle" TargetType="data:DataGridCell">
      <Setter Property="HorizontalContentAlignment" Value="Center" ></Setter>
</Style>

 

然后在要居中的列引用这个样式如

 CellStyle="{StaticResource DataGridCellStyle}"

 HeaderStyle="{StaticResource DataGridHeaderStyle}"这个是列头

DataGrid增加排序号,自增长列:

参考:http://www.silverlightchina.net/html/tips/2012/1203/20594.html

 

 <sdk:DataGridTemplateColumn Header="序号" Width="45">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock  TextAlignment="Center" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        </sdk:DataGridTemplateColumn>

 

DataGrid增加事件LoadingRow事件:LoadingRow="wryGrid_LoadingRow"

后台代码:

  private void wryGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            int index = e.Row.GetIndex();
            var cell = wryGrid.Columns[0].GetCellContent(e.Row) as TextBlock;
            cell.Text = (index + 1).ToString();
        }

 

 

 

 

posted @ 2014-04-24 11:14  高山-景行  阅读(543)  评论(0编辑  收藏  举报