允许在WPF DataGrid中滚动RowDetail内容的方法

时间:2023-01-27 19:38:05

I have a datagrid that displays items with two columns of text row data and a larger free text detail that I'm displaying via a rowdetails template with just a border and a textblock.

我有一个数据网格,显示带有两列文本行数据的项目和一个更大的*文本细节,我通过一个只带边框和文本块的rowdetails模板显示。

The problem I have is that the text details are often larger than the area allowed for the grid. The default scroll behaviour of the datagrid means that the users can't view the whole detail as the scroll jumps to the next item. If I resolve this by using

我遇到的问题是文本细节通常大于网格允许的区域。数据网格的默认滚动行为意味着当滚动跳转到下一个项目时,用户无法查看整个细节。如果我通过使用解决这个问题

ScrollViewer.CanContentScroll="False"

then the datagrid becomes unusably slow with significant numbers of rows as the virtualization is turned off.

然后,当关闭虚拟化时,数据网格变得非常慢,并且有大量行。

I did think that I could get around this by wrapping the rowdetail in a scrollviewer, but that doesn't work as the detail area isn't constrained to the render area.

我确实认为我可以通过将rowdetail包装在scrollviewer中来解决这个问题,但这不起作用,因为细节区域不限于渲染区域。

So, can anyone offer some usable options? My WPF knowledge is pretty minimal, so apologies if there's some obvious way of solving this.

那么,有人可以提供一些有用的选择吗?我的WPF知识非常少,如果有一些明显的解决方法,那么道歉。

Edit: RowDetailsTemplate

编辑:RowDetailsTemplate

<DataGrid.RowDetailsTemplate>
    <DataTemplate >
        <Border Background="Gray"
                Padding="5,5,5,5" CornerRadius="5">
            <TextBlock Background="Transparent" 
                       Foreground="White" 
                       TextWrapping="Wrap"
                       Text="{Binding Text}"/>
        </Border>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

1 个解决方案

#1


8  

One way to add a ScrollViewer for RowDetails is by specifying a MaxHeight for the RowDetails like this

为RowDetails添加ScrollViewer的一种方法是为RowDetails指定MaxHeight,就​​像这样

<DataGrid ...>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Grid MaxHeight="75">
                <ScrollViewer>
                    <Border HorizontalAlignment="Stretch" CornerRadius="5" Background="Black" Margin="5" Padding="5">
                        <TextBlock Text="{Binding RowDetails}" Foreground="#509CD5" FontSize="20" Width="300" TextWrapping="Wrap"/>
                    </Border>
                </ScrollViewer>
            </Grid>                    
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
    <!-- ... -->
</DataGrid>

#1


8  

One way to add a ScrollViewer for RowDetails is by specifying a MaxHeight for the RowDetails like this

为RowDetails添加ScrollViewer的一种方法是为RowDetails指定MaxHeight,就​​像这样

<DataGrid ...>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Grid MaxHeight="75">
                <ScrollViewer>
                    <Border HorizontalAlignment="Stretch" CornerRadius="5" Background="Black" Margin="5" Padding="5">
                        <TextBlock Text="{Binding RowDetails}" Foreground="#509CD5" FontSize="20" Width="300" TextWrapping="Wrap"/>
                    </Border>
                </ScrollViewer>
            </Grid>                    
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
    <!-- ... -->
</DataGrid>