C# WPF的DataGrid无法列标题居中,求解决 5C

工程代码
这个是我的实验代码,在我这个基础上修改,谢谢了,网上试了很多方法都没用

3个回答


<br> <Setter Property="HorizontalContentAlignment" Value="Center"><br> </Setter><br>

qq_25211827
Dream丶飞 你把工程上传,我下载好了就采纳
大约 2 年之前 回复
qq_25211827
Dream丶飞 昨天有项目忙,没上论坛
大约 2 年之前 回复
qq_25211827
Dream丶飞 没有文件啊
大约 2 年之前 回复

我把整个MainWindows.xaml的代码都发给你,你复制粘贴进去就好了。请给分。

 <Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <Grid.Resources>
            <Style x:Key="GeneralTextBlockStyle" TargetType="TextBlock">
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontFamily" Value="Calibri"/>
                <Setter Property="Foreground" Value="Gray"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
            <Style x:Key="HeaderTextBlock" TargetType="TextBlock" 
                   BasedOn="{StaticResource GeneralTextBlockStyle}">
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="Margin" Value="5,0"/>
            </Style>
            <DataTemplate x:Key="HeaderTemplateCenterAlign">
                <Border>
                    <TextBlock Style="{StaticResource HeaderTextBlock}" HorizontalAlignment="Center"
                               Text="{Binding Path=Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                </Border>
            </DataTemplate>
            <Style x:Key="CellTextBlockCenterAlign" TargetType="{x:Type TextBlock}">
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="3,0"/>
            </Style>
        </Grid.Resources>
        <DataGrid HorizontalAlignment="Left" Height="338" VerticalAlignment="Top" Width="792" Background="White" FontWeight="Bold"
                  CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{x:Null}" CanUserSort="False" ClipboardContentBinding="{x:Null}" Header="选择"
                                        HeaderTemplate="{StaticResource HeaderTemplateCenterAlign}" ElementStyle="{StaticResource CellTextBlockCenterAlign}"/>
                <DataGridTextColumn Binding="{x:Null}" CanUserSort="False" CanUserReorder="False" CanUserResize="False"
                                    ClipboardContentBinding="{x:Null}" FontWeight="Bold" Header="线束规格"  Width="100"
                                    HeaderTemplate="{StaticResource HeaderTemplateCenterAlign}" ElementStyle="{StaticResource CellTextBlockCenterAlign}"/>
                <DataGridTextColumn Binding="{x:Null}" CanUserSort="False" CanUserReorder="False" CanUserResize="False"
                                    ClipboardContentBinding="{x:Null}" Header="线长" Width="100"
                                    HeaderTemplate="{StaticResource HeaderTemplateCenterAlign}" ElementStyle="{StaticResource CellTextBlockCenterAlign}"/>
            </DataGrid.Columns>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="DataGridColumnHeader">
                                <Border Background="White" Height="25" 
                                        BorderBrush="LightGray" BorderThickness="0,1,1,1">
                                    <ContentPresenter />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>
    </Grid>
</Window>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐