WPF DataGrid - 为什么额外的列 [英] WPF DataGrid - Why the extra column

查看:25
本文介绍了WPF DataGrid - 为什么额外的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序,它使用 DataGrid 来显示一些数据.当我运行程序时,会有一个额外的列,如下所示:

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:

这是我在 VS2010 中设计时的样子

Here is what it looks like when I design it in VS2010

我已经关闭了数据网格上的 AutoGenerateColumns 并单独指定了列(这是一个用户控件):

I have turned off AutoGenerateColumns on the data grid and specified the columns individually as such (this is a user control):

<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,它使用注入来显示视图(这是一个常规窗口):

I also have a MainWindowView that uses injection to display the views as such (this is a regular window):

<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 根,因为我希望窗口足够大以容纳网格和按钮.但是,当我运行该程序时,我在数据网格中获得了一个额外的列,它使程序窗口的大小(宽度和高度)大约是 EmployeeHoursView 所需的两倍.我该如何编码,以便我的应用程序窗口对于 E​​mployeeHoursView 来说刚好足够大,而无需提供特定值?是什么导致出现此附加列?

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?

推荐答案

额外的列"实际上只是未使用的空间.您的每一列都定义了一个 Width 值,因此一旦它们被分配,就会有剩余空间.

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.

如果您想摆脱该空间,请至少将其中一列设为 * 列,以便它可以伸展以填充可用空间.

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.

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

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.

如果您不希望控件拉伸,请确保将其(或其父控件)的水平/垂直对齐方式设置为 Stretch 以外的其他内容

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

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

这篇关于WPF DataGrid - 为什么额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆