WPF DataGrid - 为什么是额外的列

时间:2011-12-13 18:34:50

标签: wpf c#-4.0 user-controls wpfdatagrid

我有一个使用DataGrid来显示某些数据的WPF应用。当我运行该程序时,还有一个附加列,如下所示: enter image description here

以下是我在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指定为Auto Size root,因为我希望窗口足够大以容纳网格和按钮。但是,当我运行程序时,我在数据网格中得到一个额外的列,它使程序窗口大约是EmployeeHoursView需要的两倍(宽度和高度)。我如何对其进行编码,使得我的应用程序窗口对于EmployeeHoursView来说足够大而不提供特定值?导致此附加列出现的原因是什么?

4 个答案:

答案 0 :(得分:41)

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

如果您想要删除该空格,请至少将其中一列设为*列,以便拉伸以填充可用空间。

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

如果你不希望你的控件伸展,那么一定要将它(或它的父级)水平/垂直对齐设置为拉伸以外的其他东西

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

答案 1 :(得分: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*" />

答案 2 :(得分:0)

在XAML中放入 ColumnWidth =“ *” ,肯定可以。

<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>

答案 3 :(得分:0)

您在左侧看到的区域是行标题。将HeadersVisibility设置为“ Column”或“ None”(取决于您要的是)

相关问题