开发者

Changing viewport of datagrid

I need to change viewport of datagrid to maximum so all rows are propagated in constructor. So I used Scrollview with row height set to auto.

<ScrollViewer>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>

开发者_如何学运维        <DataGrid>

        </DataGrid>
    </Grid>
</ScrollViewer>

This propagates all controls but now when I use scrollbar it also moves header row. I need that the scrollbar will effect only datagrid.


I dont understand this statement

... need to change viewport of datagrid to maximum so all rows are propagated in constructor. So I used Scrollview with row height set to auto...

... where is the Viewport setting in the code? Also what is this constructor? Your terminologies are confusing....

However I can assume what you want is the datagrid to fully render its rows (without extra space below rows) and occupy that entriely over the grid panel. But because you have used a scroll viewer abone grid, it will scroll datagrid headers as well.

If you use "Snoop" you will find that wpf datagrid has a scroll viewer as its visual child and it has a Grid panel in its own template, there the ScrollContentPresenter is under a Grid.Row which has RowDefinition that has Height as asterick (*).

Using visual children extraction method given below, accesss that Grid descendent and change its height to auto.

  (GetVisualChild<Grid>(myDataGrid)).RowDefintions[1].Height="Auto"

Method to snoop through all children of a parent...

    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
              Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
              child = v as T;
              if (child == null) child = GetVisualChild<T>(v);
              if (child != null) break;
        }

        return child;
    }  

The XAML way

Override the dataGrid Control template...

   <Style TargetType="{x:Type DataGrid}">
       <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="{x:Type DataGrid}">
       <Border Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              SnapsToDevicePixels="True"
              Padding="{TemplateBinding Padding}">
        <ScrollViewer   Focusable="false"
                        Name="DG_ScrollViewer">
          <ScrollViewer.Template>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/> <!--This changed to Auto from '*'-->
                  <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <!--Left Column Header Corner -->
                <Button Command="{x:Static dg:DataGrid.SelectAllCommand}"
                        Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=CellsPanelHorizontalOffset}"
                        Template="{StaticResource SelectAllButtonTemplate}"
                        Focusable="false"
                        Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=HeadersVisibility, Converter={x:Static dg:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static dg:DataGridHeadersVisibility.All}}" />
                <!--Column Headers-->
                <dgp:DataGridColumnHeadersPresenter Grid.Column="1" 
                                                   Name="PART_ColumnHeadersPresenter"
                                                   Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=HeadersVisibility, Converter={x:Static dg:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static dg:DataGridHeadersVisibility.Column}}"/>

                <!--DataGrid content-->
                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1" Grid.ColumnSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" />

                <ScrollBar Grid.Row="1" Grid.Column="2" Name="PART_VerticalScrollBar"
                                         Orientation="Vertical"
                                         Maximum="{TemplateBinding ScrollableHeight}"
                                         ViewportSize="{TemplateBinding ViewportHeight}"
                                         Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>

                <Grid Grid.Row="2" Grid.Column="1">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/>
                    <ColumnDefinition Width="*"/>
                  </Grid.ColumnDefinitions>
                  <ScrollBar Grid.Column="1"
                             Name="PART_HorizontalScrollBar"
                             Orientation="Horizontal"
                             Maximum="{TemplateBinding ScrollableWidth}"
                             ViewportSize="{TemplateBinding ViewportWidth}"
                             Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                             Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

                </Grid>
              </Grid>
            </ControlTemplate>
          </ScrollViewer.Template>
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </ScrollViewer>
      </Border>
             </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜