WPF - 具有Width =“*”的DataGrid列,但适合内容的MinWidth [英] WPF - DataGrid Column with Width="*", but MinWidth to Fit Contents
问题描述
将一组 DataGrid
列的最佳/正确的方法是具有比例宽度(宽度=*),但要使其MINIMUM宽度为最小的内容宽度?目前,如果我使用 Width =*
,那么这些列保持准确的比例,但是如果列太薄,内容就会被裁剪。如果我使用 Width =Auto
,那么列的大小本身就完全符合他们的内容,但这使得它们各不相同。
What would be the best/right way to have a set of DataGrid
columns have proportional width (Width="*"), but to have their MINIMUM width be at least the width of their content? At the moment, if I use Width="*"
, then the columns stay exactly proportional, but content gets cropped if the columns get too thin. If I use Width="Auto"
, then the columns size themselves perfectly to their content, but this makes them all different sizes.
我想要的是真正的两个组合,如Width =*,MinWidth =Auto,所以当有额外的宽度时,列将全部空间到相等的宽度,但是当网格变小,内容不会被裁剪。
What I want is really a combination of the two... like Width="*", MinWidth="Auto", so that when there's extra width the columns will all space out to equal widths, but when the grid is made smaller, the content never gets cropped.
可惜的是, MinWidth =Auto
不存在,所以我想我需要绑定列的 MinWidth
属性,但很难弄清楚我将绑定到什么。
Sadly, MinWidth="Auto"
doesn't exist, so I guess I need to bind the column's MinWidth
property, but it's hard to figure out exactly what I would bind it to.
所以...如何告诉WPFMinWidth =列最宽的内容的宽度?
So... how do I tell WPF "MinWidth = the width of the column's widest piece of content"?
任何帮助赞赏!
谢谢,
Andy
推荐答案
我知道它有点迟了,但是我发现了你的问题并编写了一个纯XAML解决方案。
I know its a bit late, but I found your question and programmed a pure-XAML solution.
<ColumnDefinition Width="42*" MinWidth="{Binding Path=ActualWidth, ElementName=projectInfoHeader }"/>
其中 ElementName
指向控制大部分空间。当然这只是可能与元素有关,宽度有限。
如果你为一个 GroupBox
做例如,你可以调整它的大小,而不是调整到更大的宽度。
Where the ElementName
points to the control taking up most of the space. Of course thats only possible to do with elements, that do have a limited width.
If you do it for example for a GroupBox
, than you can resize only to larger width and never resize to smaller one.
如果你有几个候选人的价值 MinWidth
,你需要写一个 IMultiValueConverter
,它需要一个对象[],解析它来浮动,并返回最大值(它只是一个linq查询,如果你只使用它自己,不需要处理转换器的坏的使用)
If you have several candidates for the value of MinWidth
, you need to write yourself a IMultiValueConverter
, which takes an object[], parses it to floats, and returns the maximum (its just 1 linq query if you use it only yourselves and don't need to handle bad usage of the converter)
这种方式还支持动态更改 MinWidth
。
This way also supports dynamic changing of the MinWidth
.
这篇关于WPF - 具有Width =“*”的DataGrid列,但适合内容的MinWidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!