2013-03-19 110 views
0

我有一个DataGrid定制的DataTemplate如下用户控件: -WPF DataGrid行高度符合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列中的文本块可能会有所不同。

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

如何使Datagrid收缩和扩大行数据绑定减少/增加?

Grey Area Below the last row of datagrid row

回答

1

你需要改变你的行定义

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

这将适合DataGrid的内容和它不会出现额外的空间。

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

另外,

考虑使用ItemsControl的,而不是数据网格中,你有过更多的控制权。

+0

非常感谢您的帮助。 – 2013-06-27 16:13:39