如何使 WPF 数据网格中的最后一列始终占据所有左侧空间? [英] How to make the last column in WPF datagrid take all the left space, always?

查看:18
本文介绍了如何使 WPF 数据网格中的最后一列始终占据所有左侧空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准 WPF 4 数据网格.

Standard WPF 4 Datagrid.

假设我有 200 像素宽和 2 列的数据网格.我希望列始终占据整个空间,这意味着如果用户将第一列的大小调整为 50 像素,则最后一列将为 150.

Let' say I have datagrid 200 pixels wide, and 2 columns. I would like the columns take always entire space, meaning if the user resizes the first column to 50 pixels, the last one would be 150.

最初我将第一列的宽度设置为 100 像素,最后一列设置为 *(在 XAML 中).

Initially I set width 100 pixels for the 1st column, and * for the last one (in XAML).

我认为问题在于删除了虚拟的第 3 列,如下所述:

I thought the problem is with removing the virtual, 3rd column, as explained here:

http://wpf.codeplex.com/Thread/View.aspx?线程 ID=58939

但实际上没有区别 - 尽管如此,在调整列大小时,我会在右侧获得一些额外的空间 - 使用虚拟列,它是一个虚拟列(默认为白色),没有它,它是空白空间(默认为灰色).

but there is no difference really -- still, when resizing the columns, I get some extra space on right -- with virtual column, it is a virtual column (white color by default), without it, it is empty space (gray by default).

问题:如何强制约束,无论用户如何调整列的大小,

QUESTION: how to enforce the constraint, that no matter how the user resizes the columns,

sum(columns width)==datagrid width

?

是的,我使用 WPF 4.

Yes, I use WPF 4.

解决方法

我将其中一个答案标记为解决方案,但实际上由于 WPF 设计,它不是解决方案.这只是 WPF 最多可以做的,而且不是很好——首先,列的选项 CanUserResize 意味着真正的 IsResizeable,并且这个选项在打开时与设置为 * 的 Width 相矛盾.所以如果没有一些真正聪明的技巧,你最终会得到:

I marked one of the answers as solution, but actually it is not a solution due to WPF design. It is simply what WPF can do at best, and it is not very good -- first of all the option CanUserResize for column means really IsResizeable and this option when turned on contradicts Width set to *. So without some really smart trick you end up with:

  • datagrid 最后一列在表面上可调整大小但实际上并非如此,并且右侧显示的空间很小(即虚拟列不可调整大小)--对于最后一列:CanUserResize=true, Width=*

  • datagrid which last column in superficially resizable but in fact it is not, and little space on right is shown (i.e. the virtual column is not resizable) -- for last column: CanUserResize=true, Width=*

最后一列不能被用户调整大小的数据网格,它会相应地显示出来,最初没有显示右侧的空间,但是当用户调整数据网格的任何元素的大小时可以显示——对于最后一列:CanUserResize=false, 宽度=*

datagrid which last column cannot be resized by user and it is shown accordingly, initially no space on right is shown, but it can be shown when user resizes any element of datagrid -- for last column: CanUserResize=false, Width=*

到目前为止,我发现 WPF 数据网格存在两个问题:

So far I can see two problems with WPF datagrid:

  • 误导性命名
  • 特征的矛盾

我仍然很清楚如何真正解决这个问题.

I am still all ears to how really solve this issue.

推荐答案

将数据网格的宽度设置为自动".您允许列在网格本身内正确调整大小,但您已将宽度硬连线为 200.

Set the width for the data grid to "Auto". You're allowing the columns to resize correctly within the grid itself, but you've hard-wired the width to 200.

更新:根据@micas 的评论,我可能误读了.如果是这种情况,请尝试对左列的宽度使用 100,对右列使用 100*(注意星号).这会将右列的宽度默认为 100,但允许其调整大小以填充网格.

UPDATE: Base on @micas's comment, I may have misread. If that's the case, try using 100 for the left column's width and 100* for the right column (note the asterisk). This will default the width for the right column to 100 but allow it to resize to fill the grid.

这篇关于如何使 WPF 数据网格中的最后一列始终占据所有左侧空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆