Bri*_*nKE 27 wpf user-controls wpfdatagrid c#-4.0
我有一个DataGrid用于显示一些数据的WPF应用程序.当我运行该程序时,还有一个附加列,如下所示:
这是我在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>
Run Code Online (Sandbox Code Playgroud)
我还有一个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>
Run Code Online (Sandbox Code Playgroud)
在设计器中,我已将MainWindowView和EmployeeHoursView指定为自动大小根,因为我希望窗口足够大以容纳网格和按钮.但是,当我运行程序时,我在数据网格中得到一个额外的列,它使程序窗口大约是EmployeeHoursView需要的两倍(宽度和高度).我如何对其进行编码,使得我的应用程序窗口对于EmployeeHoursView来说足够大而不提供特定值?导致此附加列出现的原因是什么?
Rac*_*hel 41
"额外列"实际上只是未使用的空间.每个列都定义了一个Width值,因此一旦它们被分配,就会留下空间.
如果要删除该空间,请至少将其中一列作为*列,以便拉伸以填充可用空间.
我最好猜测为什么它在Visual Studio中看起来很正常可能是因为你将设计器宽度设置为小于运行时宽度的东西.如果它更大,你会看到同样的事情.
如果你不想让你的控件伸展,那么一定要将它的(或它的父级)水平/垂直对齐设置为拉伸以外的其他东西
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<ItemsControl ItemsSource="{Binding ViewModels}" Margin="3" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
小智 6
将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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20527 次 |
| 最近记录: |