欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF DataGrid设置字体居中

程序员文章站 2022-07-14 08:50:39
...

xaml:

先加
<Window.Resources>
        <ResourceDictionary>
            <Style x:Key="dgCell" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Center"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

再加:ElementStyle="{StaticResource dgCell}"

 <DataGrid Name="dgBuild" ItemsSource="{Binding}"  HorizontalScrollBarVisibility="Hidden"  AutoGenerateColumns="False"  CanUserReorderColumns="False" HeadersVisibility="None"  GridLinesVisibility="All"  HorizontalGridLinesBrush="#000" IsReadOnly="True"  HorizontalAlignment="Center" >
                <DataGrid.Columns   >
                    <DataGridTextColumn Width="*"   Binding="{Binding ItemName}" ElementStyle="{StaticResource dgCell}"/>
                    <!--<DataGridTextColumn Binding="{Binding Class}"/>-->
                    <DataGridTextColumn Width="*" Binding="{Binding Data}"  ElementStyle="{StaticResource dgCell}"/>
                </DataGrid.Columns>
            </DataGrid>
相关标签: WPF wpf datagrid