当列宽缩小时,WPF DataGrid 不会缩小
我在 WPF 中使用 DataGrid,并希望它缩小以仅适合其列的宽度。它在初始渲染中很好地做到了这一点。当我调整列大小以使其更宽时,网格也会增长。但是,如果我调整列大小以使其再次变窄,我的列右侧会出现空白(并且我可以看到列标题灰色区域延伸到列之外。
我想让数据网格缩小其宽度与列,所以我没有得到右侧的空白我尝试调试代码,据我所知问题出在 DataGridCellsPanel 中,但我看不到任何地方可以修复宽度测量。
任何帮助将不胜感激。
I am using a DataGrid in WPF and want it to shrink to only fit the width of its columns. It does this nicely for the initial rendering. When I resize a column to make it wider the grid grows as well. But if I resize the column to make it narrower again I get white space on the right side of my column (and I can see that the column header grey area is extended beyond the columns.
I would like to have the data grid shrink its width with the columns so I don't get the white space on the right. I have tried to debug the code and as far as I can see the problem is in the DataGridCellsPanel, but I can't see anyplace to fix the width measurement.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不久前我就遇到了这个问题,我对此非常恼火,所以我做了一个丑陋的修复。它并不漂亮,但它可以完成工作。首先,只有当 Horizontal ScrollBar 不可见时这才会出现问题,因此我们需要对它的引用。一旦加载了所有 DataGridColumn(在我的例子中,所有数据都在 Xaml 中,因此 Loaded 事件),就必须运行此代码,并且它不考虑添加/删除 DataGridColumn,但这是一个简单的修复。
然后,在 Loaded EventHandler 中,我们获取 DataGrid ScrollViewer 并添加一个监听器,以监听 DataGrid 中每个 DataGridColumn 的 ActualWidthProperty 的变化。
然后我们根据所有 DataGridColumn 的大小计算 DataGrid 的大小,并添加一个常量 8.0(这通常是差异)。
如果您想出更好的方法,请告诉我:)
I had that problem to a while back and I was getting so annoyed by it that I made an ugly fix for it. It's not pretty, but it gets the job done. First, this is only a problem when the Horizontal ScrollBar is invisible so we're gonna need a reference to it. This code will have to be run once all DataGridColumns have been loaded (in my case, all in Xaml, so the Loaded event) and it doesn't take adding/removing of DataGridColumns into consideration but that's an easy fix.
Then in the Loaded EventHandler we get the DataGrid ScrollViewer and add a listener for changes in the ActualWidthProperty of every DataGridColumn in the DataGrid.
And then we compute the size of the DataGrid from the size of all DataGridColumns and add a constant of 8.0 (which is the difference normally).
If you come up with a better way of doing this then let me know :)
这是一个很好的解决方案。我稍微调整了它,以便它设置 MaxWidth 属性。这解决了网格扩展超出视觉父级限制的问题。为了更好地封装它,我还将它转换为一种行为。
这就是我最终得到的结果。
关于两个网格的宽度协调,我还有一些问题需要解决,但它看起来适用于一个。
That's a nice solution. I have tweaked it slightly so that it sets the MaxWidth property instead. This solves the problem of the grid expanding beyond the constraints of the visual parent. I also converted it into a behavior instead in order to encapsulate it better.
This is what I ended up with.
I still have some things to iron out about width coordination of two grids, but it looks to work for one.
您的两种方法似乎都存在一些小问题。当我将最左边的列拖到右边时,整个网格正在调整大小/滚动到内部(不幸的是,我没有足够的声誉来发布图像)。
所以我修改了jjrdk ResizeGrid函数,所以它计算最后一列的宽度并将其一直向左延伸。网格 HorizontalAlignment 和 HorizontalContentAlignment 必须设置为
水平对齐.拉伸。
我遇到的唯一问题是滚动条始终存在,即使所有列都已适合。
还有一个问题,当所有列都向左折叠时,它开始闪烁。
有什么办法可以真正消除这个空白吗?
莱昂
there is seems to be a slight problem with both of your approaches. When I drag the left most column to the right, the whole grid is getting resized/ rolled inside (unfortunatly I don't have enough reputation to post the image).
So I have modified jjrdk ResizeGrid function, so it calculate the last column width and extends it all the way to the left. The grid HorizontalAlignment and HorizontalContentAlignment must be set to
HorizontalAlignment.Stretch.
The only issue I have, is that the scroll bar is always there, even if all the columns has been fit.
There is still another issue, when all the columns are collapsed to the left, it starts flickering.
Is there anything that can be done, to really get rid of this white space?
Leon