我有一个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