wpf 如何隐藏数据网格中的最后一条水平线?

kwvwclae  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(147)
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace MergeGridTest
{
    /// <summary>
    /// test1window.xaml 的交互逻辑
    /// </summary>
    public partial class test1window : Window
    {

        DrawingVisual dv = new DrawingVisual();
        public test1window()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var Doctors = new List<Doctor>()
            {
                new Doctor(){Name="Zhang",Score=15,Address="Chengdu",Dept="Neike"},
                new Doctor(){Name="Zhang",Score=18,Address="Chengdu",Dept="Neike"},
                new Doctor(){Name="Zhang",Score=17,Address="Chengdu",Dept="Neike"},
                new Doctor(){Name="Liu",Score=15,Address="Chengdu",Dept="Thke" },
                new Doctor(){Name="Liu",Score=18,Address="MianYang",Dept="Thke"},
                new Doctor(){Name="Liu",Score=17,Address="MianYang",Dept="Thke"}
            };
            TestGrid.ItemsSource = Doctors;
        }
    }
    class Doctor
    {
        public string Name { get; set; }
        public int Score { get; set; }
        public string Address { get; set; }
        public string Dept { get; set; }
    }
}

个字符


https://note.youdao.com/yws/public/resource/ae451287f619092cc8b0485de1ddc297/xmlnote/AC1E5BD45A1C46A190271C395E0BBAA2/23392
如图所示,我不想显示最后一行(最后一条红线)下的线在这个数据网格,但其他水平线显示正常,怎么做?
数据网格的行设置是针对所有行的,所以我不能只设置最后一行的行视觉样式。

bxjv4tth

bxjv4tth1#

您可以通过以下步骤来实现这一点:
1.将DataGridGridLinesVisibility属性设置为None。(隐藏所有水平和垂直网格线)
2.将DataGridTextColumn替换为DataGridTemplateColumn,并将Border Package 的单元格内容放入DataGridTemplateColumnCellTemplate
3.将BorderBorderThickness设置为"0,1,0,0",将Margin设置为"-1",将BorderBrush设置为您喜欢的任何颜色
下面是一个可能的基于原始代码的示例代码段:

<Window
        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:MergeGridTest"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Class="MergeGridTest.test1window"
        mc:Ignorable="d"
        Title="test1window" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <DataGrid Background="LightBlue" GridLinesVisibility="None" x:Name="TestGrid" AutoGenerateColumns="False" Margin="50" IsReadOnly="True" 
                  CanUserAddRows="False" ItemsSource="{Binding}" 
                  RowHeight="30" HorizontalGridLinesBrush="#FFCB0202" VerticalGridLinesBrush="{x:Null}" VerticalContentAlignment="Center">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderThickness="0,1,0,0" BorderBrush="Black" Margin="-1">
                                <TextBlock Text="{Binding Name}"/>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Score">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderThickness="0,1,0,0" BorderBrush="Black"  Margin="-1">
                                <TextBlock Text="{Binding Score}"/>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Address">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderThickness="0,1,0,0" BorderBrush="Black"  Margin="-1">
                                <TextBlock Text="{Binding Address}"/>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

字符串

相关问题