WPF中如何移除DataGrid最后一列空列?

问题描述

在使用WPF进行应用开发,使用DataGrid制表时总是在最后面有一个空列, 例如给定如下代码:

<DataGrid
	Name="datagrid"
	Grid.Row="1"
	AlternatingRowBackground="Black"
	HeadersVisibility="All">
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="Gray" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="0,0,1,2" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Name}"
        Header=" 名称 " />
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Type}"
        Header=" 类型 " />
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Value}"
        Header=" 描述 " />
    </DataGrid.Columns>
</DataGrid>

界面显示为:

在最后面产生了一个空列,现希望去除该空列。

 

解决方案

其实这个“空列”并不是真的有一个列,而是整个表格被水平拉伸的缘故,解决方法也很简单,给表格加一个左对齐就好了:

<DataGrid
	Name="datagrid"
	Grid.Row="1"
	AlternatingRowBackground="Black"
    <!--添加这一句-->
    HorizontalAlignment="Left" 
	HeadersVisibility="All">
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="Gray" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="0,0,1,2" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Name}"
        Header=" 名称 " />
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Type}"
        Header=" 类型 " />
        <DataGridTextColumn
        Width="150"
        Binding="{Binding Value}"
        Header=" 描述 " />
    </DataGrid.Columns>
</DataGrid>

效果如下图:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值