.net 删除 WPF DataGrid 中的空白列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18215068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the blank column in a WPF DataGrid
提问by Babak.Abad
I use a DataSet to populate a DataGrid in WPF (C#). The result is:
我使用 DataSet 在 WPF (C#) 中填充 DataGrid。结果是:


I want to remove blank column at left side. And I want to share remaing space to columns. Expected result is:
我想删除左侧的空白列。我想与列共享剩余空间。预期结果是:


My XAML code is:
我的 XAML 代码是:
<Window x:Class="RFID.CareerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CareerWindow" Height="356" Width="404">
<Grid>
<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="25,10,0,0" VerticalAlignment="Top" Height="306" Width="355" EnableRowVirtualization="false" EnableColumnVirtualization="false" FontFamily="2 badr" FontSize="20" FlowDirection="RightToLeft" CanUserAddRows="False" CanUserReorderColumns="False"/>
</Grid>
</Window>
回答by Nitesh
Avoid setting static Height and Width.
避免设置静态高度和宽度。
Use ColumnWidth="*"to share the space between your DataGridColumns
使用ColumnWidth="*"分享您的DataGridColumns之间的空间
<DataGrid x:Name="dg1" ColumnWidth="*"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,10,0,0"
EnableRowVirtualization="false" EnableColumnVirtualization="false"
FontFamily="2 badr" FontSize="20" FlowDirection="RightToLeft"
CanUserAddRows="False" CanUserReorderColumns="False" />
回答by daniele3004
You can set the last column or one column of your Gridwith
您可以设置的最后一列或您的一列Grid用
<DataGridTextColumn Header="Surname"
Width="*"
Binding="{Binding Path=Surname,Mode=TwoWay}" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

