WPF DataGrid 单元格居中,头部居中,点击行改变背景色。

我得全局样式都写在了App.XAML文件下的ResourceDictionary里,方便全局引用

DataGrid样式和点击改变行背景色的触发器(BasedOn继承的是UI框架的样式,若无则删除,触发器还有鼠标移动事件等,按需自行修改添加)

复制代码
<Style x:Key="MyDataGrid"  TargetType="DataGrid" BasedOn="{StaticResource MaterialDesignDataGrid}">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="/Images/tb_1.png"/>
        </Setter.Value>
    </Setter>
    <!--网格线颜色-->
    <Setter Property="HorizontalGridLinesBrush">
        <Setter.Value>
            <SolidColorBrush Color="LightBlue"/>
        </Setter.Value>
    </Setter>
    <Setter Property="VerticalGridLinesBrush">
        <Setter.Value>
            <SolidColorBrush Color="LightBlue"/>
        </Setter.Value>
    </Setter>
    <!--行点击事件样式触发-->
    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="DataGridRow" BasedOn="{StaticResource MaterialDesignDataGridRow}">
                <Setter Property="Background" Value="Transparent" />
                <Style.Triggers>
                    <!--行点击后背景色改变,也可改变其他行属性。可添加其他事件触发器-->
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#2368DE" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="IsReadOnly" Value="True"/>

</Style>

单元格内容居中:

复制代码
<!--单元格样式触发,得单独出来写在DataGrid样式下无效-->
<Style TargetType="DataGridCell" x:Key="MyDataGridCell" BasedOn="{StaticResource MaterialDesignDataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#2368DE"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=Content.Text}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid>
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" />
                    <!--核心在这,若直接设置DataGridCell的VerticalAlignment的属性居中,则会出现下划线-->
                    <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

头部居中:

复制代码
 <Style  x:Key="MyDataGridColumnHeader" TargetType="DataGridColumnHeader" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
     <Setter Property="HorizontalContentAlignment" Value="Center" />
 </Style>

头部样式和单元格样式写在DataGrid样式下Setter设置其相应的属性无效的原因知道的麻烦留一下言。

使用:

复制代码
        <DataGrid Grid.Row="2" 
                  ColumnHeaderStyle="{StaticResource MyDataGridColumnHeader}"
                  CellStyle="{StaticResource MyDataGridCell}"
                  Style="{StaticResource MyDataGrid}"
                  AutoGenerateColumns="False" 
                  x:Name="MyDataGrid" 
                  CanUserAddRows="False"
                 ScrollViewer.CanContentScroll="True"
                 VerticalScrollBarVisibility="Auto"
                 HorizontalScrollBarVisibility="Auto"
                 ItemsSource="{Binding 自己的集合}">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{Binding 自己的对象属性}" Header="选择">
                    <DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                        </Style>
                    </DataGridCheckBoxColumn.HeaderStyle>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn   Binding="{Binding 自己的对象属性}" Header="ID" Visibility="Collapsed" Width="1*" />
               
                <DataGridTemplateColumn Header="图片">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding 自己的对象属性}" Width="24" Height="24" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <ToolTipService.ToolTip>
                                    <Image Source="{Binding 自己的对象属性}" Width="100" Height="100"/>
                                </ToolTipService.ToolTip>
                            </Image>
                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="UserAction"  Header="操作" Width="160">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Button  Content="删除"   BorderBrush="Transparent"  Click="Delete"
   HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                    <Button.Background>
                                        <ImageBrush ImageSource="/Images/btn1.png"/>
                                    </Button.Background>
                                </Button>
                                <Button Margin="12,0,0,0"  Content="编辑"  BorderBrush="Transparent" Click="Edit"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
                                    <Button.Background>
                                        <ImageBrush ImageSource="/Images/btn1.png"/>
                                    </Button.Background>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

效果:

相关推荐
半夏知半秋1 小时前
Elasticsearch 分词器
大数据·学习·elasticsearch·搜索引擎·全文检索
Sui_Network2 小时前
BitGo 通过 LayerZero 将原生 WBTC 引入 Sui
大数据·人工智能·科技·去中心化·区块链
2501_941982052 小时前
赋能销售与客户服务:企业微信外部群 RPA 自动化应用实战
大数据
汽车仪器仪表相关领域2 小时前
SCG-1 增压 + 空燃比二合一仪表:涡轮改装的 “空间杀手” 与 “安全保镖”
大数据·服务器·人工智能·功能测试·安全·汽车·可用性测试
诗旸的技术记录与分享2 小时前
Flink-1.19.0源码详解10-Flink计算资源的申请与调度
大数据·flink
Lenyiin2 小时前
makefile
java·大数据·前端
武藤一雄3 小时前
C# Prism框架详解
开发语言·后端·微软·c#·.net·wpf
资深web全栈开发3 小时前
一文讲透 MySQL 崩溃恢复方案设计
大数据·人工智能
山峰哥3 小时前
现代 C++ 的最佳实践:从语法糖到工程化思维的全维度探索
java·大数据·开发语言·数据结构·c++
努力搬砖的咸鱼3 小时前
API 网关:微服务的大门卫
java·大数据·微服务·云原生