WPF ScrollViewer围绕DataGrid会影响列宽

编程入门 行业动态 更新时间:2024-10-28 17:17:16
本文介绍了WPF ScrollViewer围绕DataGrid会影响列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用ScrollViewer时遇到问题,该问题用于滚动包含数据网格的用户控件。没有滚动查看器,列将按我的意愿填充数据网格,但是在添加滚动查看器时,列将缩小为〜15px。我能够简化布局,并且仍然可以重现此行为。

I have a problem with a ScrollViewer that I use to scroll a user control that contains a data grid. Without the scroll viewer the columns fill the data grid as I want but when adding a scroll viewer the columns shrink to ~15px. I was able to simplify my layout and can still reproduce this behaviour.

将datagrid宽度绑定到另一个控件时,列具有其正常的显示方式,但是与datagrid上的固定宽度一样,具有令人惊讶的相同效果。 我想我不是第一个遇到这个问题的人。我该如何解决此问题,以使网格将其大小调整为可用空间并为其列指定比例宽度?

When binding the datagrid width to another control the columns have their normal with, but that has unsuprisingly the same effect as a fixed width on the datagrid. I guess I'm not the first one who has this problem. How can I work around this behaviour to have my grid adjusting its size to the available space and give it's columns a propotional width?

使用scrollviewer: ,不包括:

With scrollviewer: and without:

<Window x:Class="GridTest.MainWindow" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <ScrollViewer HorizontalScrollBarVisibility="Auto"> <Grid MinWidth="200"> <DataGrid Margin="0" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column A" Width="*"/> <DataGridCheckBoxColumn Header="Column B" Width="*"/> </DataGrid.Columns> </DataGrid> </Grid> </ScrollViewer>

推荐答案

是的,我前一段时间遇到了这个问题,我采取了一种变通办法,以防万一它有用的时候我会在这里发布

Yeah, I had this problem a while ago, I resorted to a workaround, I will post here just incase its useful

<ScrollViewer HorizontalScrollBarVisibility="Auto"> <Grid x:Name="grid" MinWidth="200"> <DataGrid Width="{Binding ElementName=grid, Path=ActualWidth}"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="Column A" Width="1*"/> <DataGridCheckBoxColumn Header="Column B" Width="1*"/> </DataGrid.Columns> </DataGrid> </Grid> </ScrollViewer>

技巧是给 DataGrid 一个宽度,在这种情况下,我将绑定到包含元素

The trick was to give the DataGrid a width, in this case I bound back to the containing element

或者如果您不使用 Horizo​​ntalScrollBar ,可以禁用它,这将允许 ScrollViewer 计算宽度,从而允许DataGrid计算宽度。

Or if you don't use the HorizontalScrollBar you can disable it, this will allow the ScrollViewer to calculate a width allowing the DataGrid to calculate a width.

更多推荐

WPF ScrollViewer围绕DataGrid会影响列宽

本文发布于:2023-11-12 00:40:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   ScrollViewer   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!