0

我有一个用户控件,它有一个带有自定义 DataTemplate 的 Datagrid,如下所示:-

<UserControl x:Class="POCSurveySystem.UI.Windows.QuestionListing"
         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>
    <Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Margin" Value="0,0,5,0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border BorderThickness="0" Background="Transparent">
                        <!-- Note: IsChecked is bound to IsSelected-->
                        <RadioButton 
                    Focusable="False" 
                    IsHitTestVisible="False" 
                    IsChecked="{TemplateBinding IsSelected}">
                            <ContentPresenter />
                        </RadioButton>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ItemsPanelTemplate x:Key="HorizontalItemsPanel">
        <VirtualizingStackPanel 
    Orientation="Horizontal" />
    </ItemsPanelTemplate>
</UserControl.Resources>
<Grid Background="AliceBlue">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="23" />
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <Label Name="GroupQuestionHeader" FontSize="14" FontWeight="Bold" FontFamily="Times New Roman" HorizontalAlignment="Left" />
        <Label Name="PageCount" FontSize="10" FontFamily="Times New Roman" HorizontalAlignment="Right"></Label>
    </StackPanel>


    <DockPanel Grid.Row="2" VerticalAlignment="Stretch">
        <DataGrid VerticalScrollBarVisibility="Disabled" VerticalAlignment="Stretch" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGridQuestion" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserAddRows="False" GridLinesVisibility="All" HorizontalGridLinesBrush="#FFDEDEDE" Height="400" MaxHeight="400">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <!--<Setter Property="Padding" Value="5" />-->
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="BorderBrush" Value="{x:Null}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>

            <DataGrid.Columns>

                <DataGridTemplateColumn Header="Question" Width="2*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding QuestionContent, Mode=OneWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="We fully Comply | We partly Comply | We do not Comply" Width="1*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <!--<ListBox 
                            BorderThickness="0" 
                            SelectedValue="{Binding MyDataListSelectedValue}" 
                            ItemContainerStyle="{StaticResource RadioButtonItemStyle}" 
                            ItemsPanel="{StaticResource HorizontalItemsPanel}" Name="OptionsRadioButtonGroup" HorizontalContentAlignment="Left"
                                Cursor="Hand" HorizontalAlignment="Left">
                                <ListBoxItem Width="90" Name="AGR"/>
                                <ListBoxItem Width="90" Name="PGR"/>
                                <ListBoxItem Name="DNR"/>
                            </ListBox>-->
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
    <StackPanel Orientation="Horizontal" Grid.Row="3">
        <Button Content="Next Page" Height="23" HorizontalAlignment="Left" Name="btnNext" VerticalAlignment="Top" Width="75" Click="btnNext_Click"  Margin="5,0,0,0" />
        <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="86,0,0,0" Name="btnSubmit" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="btnSubmit_Click" />
    </StackPanel>

</Grid>

但是,datagridview 不会随着数据行的增长而收缩/拉伸。我试图硬编码 dataGridQuestion.MinRowHeight = 100 但这不是我想要的,因为 datagrid 列中的 Textblock 可能会有所不同。

问题:如何避免在datagrid行的最后一行之后出现如下图中的灰色区域?我使用 dataGridQuestion.MinRowHeight = dataGridQuestion.Height / DataEntityList.Count 进行了测试,但它仍然存在..

随着数据绑定行的减少/增加,如何使 Datagrid 缩小和扩大?

数据网格行最后一行下方的灰色区域

4

1 回答 1

1

您需要更改行定义

        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="30"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="23" />
        </Grid.RowDefinitions>

这将使数据网格适合内容,并且不会显示额外的空间。

然后,如果你想让你的行更大,只需在 DataGrid 中设置 MinRowHeight="200"。

或者,

考虑使用 ItemsControl 而不是 DataGrid,因为您可以更好地控制它。

于 2013-03-19T08:29:53.500 回答