2011-12-13 72 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*" />