WPF数据网格 - 为什么会有额外的列

29
我有一个使用 DataGrid 显示某些数据的 WPF 应用程序。当我运行程序时,会出现一个额外的列,如下所示: enter image description here 在 VS2010 中设计时它看起来是这样的: enter image description here 我已经在数据网格上关闭了 AutoGenerateColumns,并指定了单独的列,如下所示(这是一个用户控件):
<Grid Margin="10,10,10,10">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>


    <DataGrid x:Name="EmployeeHours" AutoGenerateColumns="False" ItemsSource="{Binding EmployeeHoursLastWeek}" Width="Auto">
        <DataGrid.Columns>
            <DataGridTextColumn Header="PerceptionistID" Binding="{Binding PerceptionistID}" Width="100" />
            <DataGridTextColumn Header="Week Of" Binding="{Binding WeekOf, StringFormat={}{0:MM/dd/yyyy}}" Width="75" />
            <DataGridTextColumn Header="Regular Hours" Binding="{Binding WorkHours}" Width="100" />
            <DataGridTextColumn Header="PTO Hours" Binding="{Binding PTOHours}" Width="100" />
            <DataGridTextColumn Header="Holiday Hours" Binding="{Binding HolidayHours}" Width="100" />
        </DataGrid.Columns>
    </DataGrid>

    <Button x:Name="ImportHoursButton" Content="Import Hours" 
            Command="{Binding ImportHoursCommand}" 
            Height="25" Width="100" Margin="10"
            VerticalAlignment="Bottom" HorizontalAlignment="Right"                
            Grid.Row="1" />
</Grid>

我还有一个MainWindowView,使用注入方式来展示视图,如下所示(这是一个常规窗口):

<Window x:Class="Sidekick.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Sidekick.ViewModel"
        xmlns:vw="clr-namespace:Sidekick.View"
        Title="Sidekick">

    <!-- Typically done in a resources dictionary -->    
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:EmployeeHoursViewModel}">
            <vw:EmployeeHoursView />
        </DataTemplate>
    </Window.Resources>

    <StackPanel>
        <ItemsControl ItemsSource="{Binding ViewModels}" Margin="3" />
    </StackPanel>

</Window>

在设计器中,我将MainWindowView和EmployeeHoursView都指定为AutoSize根元素,因为我希望窗口大小刚好能容纳网格和按钮。然而,当我运行程序时,数据网格会多出一列,导致程序窗口的大小大约是EmployeeHoursView所需大小的两倍(宽度和高度都是)。如何编写代码以使我的应用程序窗口大小刚好适合EmployeeHoursView,而不需要提供具体的值?是什么原因导致了这个额外的列出现?

4个回答

44
"额外的列"实际上只是未使用的空间。每个列都定义了一个宽度值,所以一旦它们被分配了宽度,就会剩余空间。
如果你想要去掉这个空间,那就将至少一个列设为*列,这样它就会拉伸以填充可用空间。
我猜在Visual Studio中看起来正常的原因可能是因为设计器宽度设置比运行时宽度小。如果它更大,你会看到同样的情况。
如果你不想让你的控件拉伸,请确保将它(或它的父级)的水平/垂直对齐方式设置为除Stretch之外的其他选项。
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <ItemsControl ItemsSource="{Binding ViewModels}" Margin="3" />
</StackPanel>

1
但是,这不是“自动大小”的目的吗?将元素(在这种情况下是数据网格和主窗口)调整为所需大小。还有哪些地方需要调整大小的元素呢?如果我将最后一列的宽度更改为“*”,那么整个窗口的大小仍然相同,现在我有了一个超宽的列。 - BrianKE
1
从微软的MSDN网站上来看,自动调整大小的行为意味着元素将填充可用的宽度。但请注意,特定的控件通常会在其默认样式中提供默认值,除非明确重新启用,否则将禁用自动调整大小的行为。http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.width.aspx - Rachel
1
谢谢,这帮助我指向了MainWindow上的SizeToContent。将其设置为“WidthAndHeight”现在可以根据数据网格的大小正确调整MainWindow的大小。 - BrianKE
有没有办法以其他背景颜色显示那个额外的列? - Varun Mahajan
3
你可以尝试在表格中添加一个额外的空白列来利用未使用的空间,并将该列的行背景颜色设置为RowBackground - Rachel

5
在XAML中加入ColumnWidth="*",它肯定会生效。
<DataGrid x:Name="DG_FileShow" ColumnWidth="*" ItemsSource="{Binding}" 
 HorizontalAlignment="Left" VerticalAlignment="Top" Height="345" Width="654" 
 Margin="34,53,0,0" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Names" Binding="{Binding}" />
        </DataGrid.Columns>
 </DataGrid>

1

您所看到的左侧区域是行标题。将HeadersVisibility设置为"Column"或"None"(取决于您想要哪个)


1

既然是未使用的空间,另一个解决方法是使用加权宽度而不是固定宽度。你也可以采用混合方法,其中一些是固定的,一些是加权的,在这种情况下,请确保其中一个是加权的 (*) 因此,在您的代码中,它将是:

<DataGridTextColumn Header="PerceptionistID" Binding="{Binding PerceptionistID}" Width="4*" />
        <DataGridTextColumn Header="Week Of" Binding="{Binding WeekOf, StringFormat={}{0:MM/dd/yyyy}}" Width="3*" />
        <DataGridTextColumn Header="Regular Hours" Binding="{Binding WorkHours}" Width="4*" />
        <DataGridTextColumn Header="PTO Hours" Binding="{Binding PTOHours}" Width="4*" />
        <DataGridTextColumn Header="Holiday Hours" Binding="{Binding HolidayHours}" Width="4*" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接