更改数据网格的视口

发布于 2024-12-10 06:35:52 字数 528 浏览 0 评论 0原文

我需要将数据网格的视口更改为最大值,以便所有行都在构造函数中传播。所以我使用了 Scrollview 并将行高设置为自动。

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

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

        <DataGrid>

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

这会传播所有控件,但现在当我使用滚动条时,它也会移动标题行。我需要滚动条仅影响数据网格。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2024-12-17 06:35:52

我不明白这个说法

...需要将数据网格的视口更改为最大,以便所有行都
在构造函数中传播。所以我使用了 Scrollview 并将行高设置为
自动...

...代码中的Viewport设置在哪里?另外这个构造函数是什么?您的术语令人困惑......

但是我可以假设您想要的是数据网格完全渲染其行(行下方没有额外的空间)并占据网格面板上的条目。但是因为您在网格上使用了滚动查看器,所以它也会滚动数据网格标题。

如果您使用“Snoop”,您会发现 wpf datagrid 有一个滚动查看器作为其可视子项,并且在其自己的模板中有一个 Grid 面板,其中 ScrollContentPresenter 位于一个 Grid.Row,其 RowDefinitionHeight 为星号 (*)。

使用下面给出的可视子元素提取方法,访问该网格后代并将其高度更改为自动。

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

监听父级的所有子级的方法...

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

XAML 方式

覆盖 dataGrid 控件模板...

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

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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文