2011-12-13 74 views
23

我有一个WPF应用程序,它使用DataGrid来显示一些数据。当我运行的程序有如下所示的附加列: enter image description hereWPF DataGrid - 为什么额外的列

这里是个什么样子,当我在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作为自动尺寸根如我所需的窗口大到足以容纳网格和按钮。但是,当我运行程序时,我在数据网格中获得了一个额外的列,它使程序窗口大约是EmployeeHoursView需要的两倍(宽度和高度)。我如何编码这样的话,我的应用程序窗口对于EmployeeHoursView来说足够大,而不提供具体的值?是什么导致这个额外的列出现?

回答

38

“额外列”实际上只是未使用的空间。每个列都定义了一个宽度值,因此一旦它们被分配,就剩下空间了。

如果您想摆脱那个空间,请至少将其中一个列设置为*列,以便延伸以填充可用空间。

我最好的猜测,为什么它在Visual Studio中看起来很正常,可能是因为您将设计器宽度设置为小于运行时宽度的值。如果它更大,你会看到同样的事情。

如果你不想让你的控制,拉伸,那么一定要设置它的(或它的母公司)的水平/垂直对齐比拉伸

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <ItemsControl ItemsSource="{Binding ViewModels}" Margin="3" /> 
</StackPanel> 
+0

但不是点自动调整大小,以便根据需要调整元素的大小(本例中为数据网格,代理为主窗口)?一个尺寸元素将在哪里发挥作用?如果我将最后一列的宽度更改为“*”,那么总体窗口大小仍然相同,现在我有一个超宽列。 – BrianKE 2011-12-13 18:56:44

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*" />