我在WPF应用程序中遇到了开箱即用的.net 4 Datagrid用户控件的问题,但只有在Window 7操作系统上安装并运行应用程序时才会出现此问题。我们使用MVVM设计,ObservableCollection和数据绑定实现了数据网格控件。 XAML代码非常标准,如下所示:
风格:
<Style x:Key="SummaryGrid" TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
<Setter Property="Margin" Value="0,0,0,10"></Setter>
<Setter Property="GridLinesVisibility" Value="Horizontal"></Setter>
<Setter Property="BorderBrush" Value="#d4d7db"></Setter>
<Setter Property="HorizontalGridLinesBrush" Value="#d4d7db"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="RowDetailsVisibilityMode" Value="Visible"></Setter>
<Setter Property="Foreground" Value="#363636"></Setter>
<Setter Property="CanUserSortColumns" Value="False"></Setter>
<Setter Property="RowStyleSelector">
<Setter.Value>
<help:RowStyleSelector DefaultStyle="{StaticResource SummaryGridRow}" NewItemStyle="{StaticResource SummaryGridNewRow}"></help:RowStyleSelector>
</Setter.Value>
</Setter>
</Style>
用户控制:
<UserControl x:Class="BRAPClientApp.Views.VisitList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<UserControl.Resources>
</UserControl.Resources>
<StackPanel Margin="20,0,0,0" >
<TextBlock x:Name="header" x:Uid="header" HorizontalAlignment="Left" Style="{StaticResource h2}" TextWrapping="Wrap" >Scheduled Visits</TextBlock>
<DataGrid x:Name="fp" ItemsSource="{Binding Path=VisitMenuViewModel.VisitButtons}"
AutoGenerateColumns="False"
CanUserAddRows="False"
Style="{StaticResource SummaryGrid}"
CellStyle="{StaticResource SummaryGridCell}"
ColumnHeaderStyle="{StaticResource SummaryGridHeader}"
RowHeaderStyle="{StaticResource DataGridRowHeaderStyle1}"
CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Client" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.OrgName}"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Status" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.visitStatus}"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Start Date" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.startdate, StringFormat={}{0:dd/MM/yyyy}}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock >
<Hyperlink Command="{Binding Command}" CommandParameter="{Binding Path=Visit.VisitId}" >Start</Hyperlink>
<!--<Button Content="Start" Command="{Binding Command}" CommandParameter="{Binding Path=Visit.VisitId}" />-->
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
问题是,在Load事件之后,Datagrid似乎没有自己调整大小,并且在底部渲染了大量的灰色空间。我个人认为这是Datagrid对象上未报告的错误,但我想知道你们对它的看法。请看下面的截图:
答案 0 :(得分:0)
尝试使用而不是StackPanel
DockPanel
,然后按DockPanel.Dock
组织您的元素。 DockPanel
的属性LastChildFill
默认设置为true
,因此DataGrid
将填充您控件的可用空间:
<DockPanel Margin="20,0,0,0" >
<TextBlock DockPanel.Dock="Top" x:Name="header" x:Uid="header" HorizontalAlignment="Left" Style="{StaticResource h2}" TextWrapping="Wrap" >Scheduled Visits</TextBlock>
<DataGrid DockPanel.Dock="Top" x:Name="fp" ItemsSource="{Binding Path=VisitMenuViewModel.VisitButtons}"
AutoGenerateColumns="False"
CanUserAddRows="False"
Style="{StaticResource SummaryGrid}"
CellStyle="{StaticResource SummaryGridCell}"
ColumnHeaderStyle="{StaticResource SummaryGridHeader}"
RowHeaderStyle="{StaticResource DataGridRowHeaderStyle1}"
CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Client" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.OrgName}"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Status" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.visitStatus}"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource CellEditStyle}"
ElementStyle="{StaticResource CellStyle}"
Header="Start Date" Width="*" IsReadOnly="True"
Binding="{Binding Path=Visit.startdate, StringFormat={}{0:dd/MM/yyyy}}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock >
<Hyperlink Command="{Binding Command}" CommandParameter="{Binding Path=Visit.VisitId}" >Start</Hyperlink>
<!--<Button Content="Start" Command="{Binding Command}" CommandParameter="{Binding Path=Visit.VisitId}" />-->
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>