我有一個WPF應用程序,它使用DataGrid
來顯示一些數據。當我運行的程序有如下所示的附加列: WPF DataGrid - 爲什麼額外的列
這裏是個什麼樣子,當我在VS2010設計它
我已經關閉的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來說足夠大,而不提供具體的值?是什麼導致這個額外的列出現?
但不是點自動調整大小,以便根據需要調整元素的大小(本例中爲數據網格,代理爲主窗口)?一個尺寸元素將在哪裏發揮作用?如果我將最後一列的寬度更改爲「*」,那麼總體窗口大小仍然相同,現在我有一個超寬列。 – BrianKE 2011-12-13 18:56:44