简体   繁体   中英

How to style DataGridCell in WPF DataGrid?

I have datagrid with the hierarchy like this..

WPF

I want to style textblock in datagrid to have horizontal alignment to Center like picture above.

Can you help me How I can achieve this? With WPF Styling?

You could use a DataGridTemplateColumn.

<DataGrid>
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Key">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock HorizontalAlignment="Center" Text="{Binding Key}"/>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM