WPF DataGrid如何使冻结的行/列工作?

new*_*man 7 wpf datagrid scrollviewer

我创建了一个基于Grid(而不是DataGrid)的用户控件,它包含在ScrollViewer中.现在我想像DataGrid一样拥有冻结的行/列功能,但无法弄清楚如何.

有人可以告诉我一些如何在WPF DataGrid中完成它吗?

LPL*_*LPL 5

在我自己遇到这个问题之后,我想分享我迄今为止发现的东西。

DataGrid 为此使用两种不同的方法。


第一:行标题


这是简化TemplateDataGridRow

<Border x:Name="DGR_Border" ... >
    <SelectiveScrollingGrid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <DataGridRowHeader Grid.RowSpan="2"
            SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" ... />

        <DataGridCellsPresenter Grid.Column="1" ... />

        <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1"
            SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                                                           Path=AreRowDetailsFrozen, Converter={x:Static DataGrid.RowDetailsScrollingConverter},
                                                                           ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}}" ... />
    </SelectiveScrollingGrid>
</Border>
Run Code Online (Sandbox Code Playgroud)

如您所见,DataGrid使用SelectiveScrollingOrientation附加属性将 RowHeader 固定到位。如果设置(或更改)此属性,它会为元素创建一个TranslateTransform到父ScrollViewer偏移量的自适应绑定。请参阅源代码中的详细信息。


第二个:FrozenColumns


这件事发生在. 它使用一个私有类“在多个孩子的安排之间维护状态”。DataGridCellsPanel ArrangeOverride()ArrangeState

private class ArrangeState
{
    public ArrangeState()
    {
        FrozenColumnCount = 0;
        ChildHeight = 0.0;
        NextFrozenCellStart = 0.0;
        NextNonFrozenCellStart = 0.0;
        ViewportStartX = 0.0;
        DataGridHorizontalScrollStartX = 0.0;
        OldClippedChild = null;
        NewClippedChild = null;
    }

    public int FrozenColumnCount { get; set; }
    public double ChildHeight { get; set; }
    public double NextFrozenCellStart { get; set; }
    public double NextNonFrozenCellStart { get; set; }
    public double ViewportStartX { get; set; } 
    public double DataGridHorizontalScrollStartX { get; set; }
    public UIElement OldClippedChild { get; set; }
    public UIElement NewClippedChild { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

初始化状态后

private void InitializeArrangeState(ArrangeState arrangeState)
{
    DataGrid parentDataGrid = ParentDataGrid;
    double horizontalOffset = parentDataGrid.HorizontalScrollOffset;
    double cellsPanelOffset = parentDataGrid.CellsPanelHorizontalOffset;
    arrangeState.NextFrozenCellStart = horizontalOffset;
    arrangeState.NextNonFrozenCellStart -= cellsPanelOffset;
    arrangeState.ViewportStartX = horizontalOffset - cellsPanelOffset;
    arrangeState.FrozenColumnCount = parentDataGrid.FrozenColumnCount;
}
Run Code Online (Sandbox Code Playgroud)

它叫

ArrangeChild(children[childIndex] as UIElement, i, arrangeState);
Run Code Online (Sandbox Code Playgroud)

对于所有已实现的子项并计算未实现的子项/列的估计宽度。

double childSize = GetColumnEstimatedMeasureWidth(column, averageColumnWidth);
arrangeState.NextNonFrozenCellStart += childSize;
Run Code Online (Sandbox Code Playgroud)

最后,这些值将在DataGrid.

private void FinishArrange(ArrangeState arrangeState)
{
    DataGrid parentDataGrid = ParentDataGrid;

    // Update the NonFrozenColumnsViewportHorizontalOffset property of datagrid
    if (parentDataGrid != null)
    {
        parentDataGrid.NonFrozenColumnsViewportHorizontalOffset = arrangeState.DataGridHorizontalScrollStartX;
    }

    // Remove the clip on previous clipped child
    if (arrangeState.OldClippedChild != null)
    {
        arrangeState.OldClippedChild.CoerceValue(ClipProperty);
    }

    // Add the clip on new child to be clipped for the sake of frozen columns.
    _clippedChildForFrozenBehaviour = arrangeState.NewClippedChild;
    if (_clippedChildForFrozenBehaviour != null)
    {
        _clippedChildForFrozenBehaviour.CoerceValue(ClipProperty);
    }
}
Run Code Online (Sandbox Code Playgroud)

ArrangeChild(UIElement child, int displayIndex, ArrangeState arrangeState)您可以从源代码中的第 1470 行找到详细信息。


结论


不是让列被冻结那么简单。即使这会起作用(除了在整个宽度上剪切和滚动条)

<ListView ItemsSource="some rows">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Fixed"
                           Background="LightBlue" Width="300"
                           SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
                <TextBlock Grid.Column="1" Text="Scrolled"
                           Background="LightGreen" Width="300" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

这不会:

<ScrollViewer HorizontalScrollBarVisibility="Auto">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="Fixed"
                   Background="LightBlue" Width="300"
                   SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
        <TextBlock Grid.Column="1" Text="Scrolled"
                   Background="LightGreen" Width="300" />                    
    </Grid>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

原因是DataGridHelper.FindVisualParent<ScrollViewer>(element)(参见源代码中的第 149 行) inSelectiveScrollingOrientation attached property失败。也许您会找到解决方法,例如使用原始代码的副本创建您自己的附加属性,但ScrollViewer按名称获取 。否则我认为你必须从头开始做很多事情。


Sag*_*odi 0

Datagrid 列和行有一个名为“冻结”的属性

如果您想冻结列,我建议您执行以下操作

您希望它出现在选定的行或列事件上,然后在事件上获取列/行并将其标记为 Frozen = true

或者创建另一个按钮或右键单击上下文菜单,您可以在其中冻结/取消冻结当前标记的内容

列/行

希望这可以帮助