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

发布于 2024-10-02 19:54:10 字数 878 浏览 1 评论 0原文

我有一个数据网格,它显示包含两列文本行数据和一个更大的自由文本详细信息的项目,我通过仅包含边框和文本块的 rowdetails 模板显示这些详细信息。

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

ScrollViewer.CanContentScroll="False"

,那么当虚拟化关闭时,数据网格会变得非常慢,并且行数很大。

我确实认为我可以通过将 rowdetail 包装在滚动查看器中来解决这个问题,但这不起作用,因为细节区域不受渲染区域的限制。

那么,有人可以提供一些可用的选项吗?我的 WPF 知识非常少,所以如果有一些明显的方法可以解决这个问题,我深表歉意。

编辑:行详细信息模板

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

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.

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.

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

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

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

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

发布评论

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

评论(1

沉默的熊 2024-10-09 19:54:10

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

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

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