1

我在我们的 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>

问题是 Datagrid 在 Load 事件之后似乎没有调整自己的大小,并且在底部呈现了大量的灰色空间。我个人认为这是 Datagrid 对象上未报告的错误,但我想知道你们对此有何看法。请看下面的截图:

问题截图

4

1 回答 1

0

尝试使用而不是StackPanelaDockPanel然后组织你的元素DockPanel.Dock。具有默认设置为DockPanel的属性,因此将填充控件的可用空间:LastChildFilltrueDataGrid

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

于 2013-05-30T10:35:26.617 回答