WPF - 宽度 =“*”的 DataGrid 列,但 MinWidth 以适合内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3404067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - DataGrid Column with Width="*", but MinWidth to Fit Contents
提问by Andy T
What would be the best/right way to have a set of DataGrid
columns have proportional width (Width="\*"),
but to have their minimumwidth 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.
让一组DataGrid
列具有比例宽度的最佳/正确方法是什么(Width="\*"),
但让它们的最小宽度至少是其内容的宽度?目前,如果我使用Width="*"
,那么列保持完全成比例,但内容如果列太细,就会被裁剪。如果我使用Width="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.
我想要的实际上是两者的组合,例如Width="\*"
,MinWidth="Auto"
这样当有额外的宽度时,列将全部间隔为相等的宽度,但是当网格变小时,内容永远不会被裁剪。
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.
可悲的是,MinWidth="Auto"
不存在,所以我想我需要绑定列的MinWidth
属性,但很难弄清楚我会将它绑定到什么。
How do I tell WPF "MinWidth="
the width of the column's widest piece of content?
如何告诉 WPF"MinWidth="
列的最宽内容的宽度?
回答by Tomas Grosup
I know its a bit late, but I found your question and programmed a pure-XAML solution.
我知道它有点晚了,但我发现了你的问题并编写了一个纯 XAML 解决方案。
<ColumnDefinition Width="42*" MinWidth="{Binding Path=ActualWidth, ElementName=projectInfoHeader }"/>
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.
凡ElementName
指向控制占用了大部分空间。当然,这仅适用于具有有限宽度的元素。例如,如果您为 a 执行此操作GroupBox
,则只能将其调整为更大的宽度,而永远不要将其调整为更小的宽度。
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
,您需要自己编写一个IMultiValueConverter
,它接受一个对象 [],将其解析为浮点数,并返回最大值(如果您只自己使用它并且不需要,它只是 1 个 linq 查询)处理转换器的不良使用)
This way also supports dynamic changing of the MinWidth
.
这种方式也支持动态改变MinWidth
.
回答by Anon
Set Width = "Auto"
in XAML.
Width = "Auto"
在 XAML 中设置。
Then in the code:
然后在代码中:
MinWidth = ActualWidth
Width = new GridLength(1, GridUnitType.Star)
回答by Ollikat
I also had problems to size Grid columns correctly inside the GridViewColumn. There were several things that I tried but then I found the UniformGrid. It was the ultimate solution for me. It just works. I haven't knew it before...seems that it doesn't exist in VS toolbox by default (?) and thus didn't know it even exists.
我也遇到了在 GridViewColumn 中正确调整 Grid 列大小的问题。我尝试了几件事,但后来我找到了 UniformGrid。这对我来说是最终的解决方案。它只是有效。我以前不知道它......默认情况下它似乎不存在于 VS 工具箱中(?),因此甚至不知道它存在。
You can find more about UniformGrid from here.
您可以从此处找到有关 UniformGrid 的更多信息。
回答by danielcooperxyz
Give the column a name in the XAML:
在 XAML 中为列命名:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" Name="Col1"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
Then set the MinWidth
property in the code as shown below:
然后MinWidth
在代码中设置属性如下图:
public MainWindow()
{
InitializeComponent();
Col1.MinWidth = 340; //enter desired minimum width here
}