qq_25211827
Dream丶飞
采纳率25%
2018-07-24 00:14 阅读 1.1k

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

5

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • LOVE_219421 LOVE_219421 2018-07-24 01:01


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

    点赞 评论 复制链接分享
  • LOVE_219421 LOVE_219421 2018-07-24 01:09
    点赞 评论 复制链接分享
  • wyw8152 逍遥岚轩 2018-07-25 07:58

    我把整个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>
    
    
    点赞 评论 复制链接分享

相关推荐