WPF DataGrid - Why the extra column

廉价感情. 提交于 2019-11-30 16:59:54

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.

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.

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>

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*" />

Put ColumnWidth="*" in the XAML and it will surely be work.

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!