2013-03-19 136 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