如何使WPF datagrid中的最后一列占用所有的左侧空间,总是? [英] How to make the last column in WPF datagrid take all the left space, always?

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

问题描述

标准WPF 4 Datagrid。



让我们说datagrid有200像素宽,2列。我希望这些列总是占据整个空间,这意味着如果用户将第一列的大小调整为50像素,最后一列将为150.



最初设置宽度为100像素对于第1列,*代表最后一个(在XAML中)。



我认为问题是删除虚拟的第3列,如下所述: p>

http:// wpf。 codeplex.com/Thread/View.aspx?ThreadId=58939



但真的没有什么区别 - 仍然在调整列的大小时,我得到一些右侧的额外空间 - 使用虚拟列,它是一个虚拟列(默认为白色),没有它,它是空的空格(默认为灰色)。



QUESTION :如何执行约束,无论用户如何调整列的大小,

  sum (列宽)== datagrid width 



编辑



是的,我使用WPF 4。



替代方法



我将其中一个答案标记为解决方案,但实际上它不是WPF设计的解决方案。它只是WPF可以做的最好的,它不是很好 - 首先选择CanUserResize为列意味着真正IsResizeable和此选项当打开与宽度设置为*矛盾时。所以没有一些真正的聪明的技巧,你最终会得到:




  • datagrid最后一列在表面上可调整大小,但实际上不是,显示右侧的空格(即虚拟列不可调整) - 最后一列:CanUserResize = true,Width = *


  • datagrid last列不能被用户调整大小,并且相应地显示,最初没有显示右侧的空格,但是当用户调整datagrid的任何元素时,可以显示最后一列:CanUserResize = false,Width = *




到目前为止,我可以看到WPF datagrid的两个问题:




  • 误导性命名

  • 功能矛盾



我还是耳朵如何真正解决这个问题。

解决方案

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



更新:基于@ micas的评论,我可能会误会。如果是这样,请尝试使用左侧列的宽度为100,右侧列为100 *(注意星号)。这将默认右列的宽度为100,但允许其调整大小以填充网格。


Standard WPF 4 Datagrid.

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.

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

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

http://wpf.codeplex.com/Thread/View.aspx?ThreadId=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

?

Edits

Yes, I use WPF 4.

WORKAROUND

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

  • 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=*

So far I can see two problems with WPF datagrid:

  • misleading naming
  • contradiction of features

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

解决方案

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.

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 datagrid中的最后一列占用所有的左侧空间,总是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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