WPF DataGrid - 为什么额外的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8494333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid - Why the extra column
提问by BrianKE
I have a WPF app that uses DataGrid
to display some data. When I run the program there is an additional column as shown here:
我有一个DataGrid
用于显示一些数据的 WPF 应用程序。当我运行程序时,会有一个额外的列,如下所示:
Here is what it looks like when I design it in VS2010
这是我在 VS2010 中设计时的样子
I have turned off AutoGenerateColumns on the data grid and specified the columns individually as such (this is a user control):
我已经关闭了数据网格上的 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>
I also have a MainWindowView that uses injection to display the views as such (this is a regular window):
我还有一个 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>
In the designer I have specified both MainWindowView and EmployeeHoursView as Auto Size root as I want the window to be just large enough to accommodate the grid and button. However, when I run the program I get an additional column in the data grid and it makes the program window about twice as large (both width and height) as the EmployeeHoursView needs to be. How can I code this such that my application window is just large enough for the EmployeeHoursView without providing specific values? What is causing this additional column to appear?
在设计器中,我将 MainWindowView 和 EmployeeHoursView 指定为 Auto Size 根,因为我希望窗口足够大以容纳网格和按钮。但是,当我运行该程序时,我在数据网格中获得了一个额外的列,它使程序窗口的大小(宽度和高度)大约是 EmployeeHoursView 所需大小的两倍。我该如何编码,以便我的应用程序窗口对于 EmployeeHoursView 来说刚好足够大,而无需提供特定值?是什么导致出现此附加列?
回答by Rachel
The "extra column" is actually just unused space. Each of your columns define a Width value, so once they get assigned there is space left over.
“额外的列”实际上只是未使用的空间。您的每一列都定义了一个 Width 值,因此一旦它们被分配,就会有剩余空间。
If you want to get rid of that space, make at least one of your columns a *
column so it stretches to fill available space.
如果你想摆脱那个空间,至少让你的一列成为一*
列,这样它就会伸展以填充可用空间。
My best guess as to why it looks normal in Visual Studio is probably because you have the designer width set to something smaller than the runtime width. If it were larger, you'd see the same thing.
我对为什么它在 Visual Studio 中看起来正常的最佳猜测可能是因为您将设计器宽度设置为小于运行时宽度的值。如果它更大,你会看到同样的东西。
If you don't want your control to stretch, then be sure to set it's (or it's parent's) horizontal/vertical alignment to something other than Stretch
如果您不希望您的控件拉伸,请确保将其(或它的父级)水平/垂直对齐方式设置为 Stretch 以外的其他内容
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<ItemsControl ItemsSource="{Binding ViewModels}" Margin="3" />
</StackPanel>
回答by amarnath chatterjee
Well since its unused space, another way around will be use a weighted width rather than fixed one. You can use a hybrid approach as well wherein some are fixed and some are weighted, in that case ensure one is weighted (*) So in your code it will be :
好吧,由于其未使用的空间,另一种方法是使用加权宽度而不是固定宽度。您也可以使用混合方法,其中一些是固定的,一些是加权的,在这种情况下,确保一个加权(*)因此在您的代码中它将是:
<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*" />
回答by Adeel Kamran
Put ColumnWidth="*"in the XAML and it will surely be work.
将ColumnWidth="*"放在 XAML 中,它肯定会起作用。
<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>
回答by Jym
The area on the left that you are seeing is the Row Headers. Set the HeadersVisibility to either "Column" or "None" (depending on which you want)
您看到的左侧区域是行标题。将 HeadersVisibility 设置为“Column”或“None”(取决于您想要的)