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.
让一组DataGrid列具有比例宽度(宽度=“\*”),但是让它们的最小宽度至少等于内容的宽度,最好的/正确的方法是什么?此时,如果我使用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=”列最宽内容的宽度?
5 个解决方案
#1
18
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.
元素名指向占据大部分空间的控件。当然,这只可能与元素有关,因为元素的宽度是有限的。如果你对一个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,它接受一个对象[],解析它到float,并返回最大值(如果您自己使用它,并且不需要处理转换器的不良使用,那么它只需要一个linq查询)
This way also supports dynamic changing of the MinWidth
.
这种方式也支持动态变化的最小宽度。
#2
12
Set Width = "Auto"
in XAML.
设置宽度= XAML中的“Auto”。
Then in the code:
然后在代码:
MinWidth = ActualWidth
Width = new GridLength(1, GridUnitType.Star)
#3
2
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中遇到了正确大小网格列的问题。有几样东西我试过了,但后来我找到了均格线。这是我的终极解决方案。它只是工作。我以前不知道……似乎它在VS工具箱中默认不存在(?),因此不知道它是否存在。
You can find more about UniformGrid from here.
您可以从这里找到更多关于均匀网格的信息。
#4
1
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
}
#5
0
Simply you can use below code to set max or min width of the column
只需使用下面的代码设置列的最大或最小宽度
<ColumnDefinition Width="*" MaxWidth="40"/>
This will take the available space but it will limit to max 40px
这将占用可用空间,但将限制为最大40px
you can do the same with minwidth also
你也可以对minwidth进行同样的操作
<ColumnDefinition Width="*" MinWidth="50"/>
This will take the available space but always make sure it has 50px space at least. Hope this will help.
这将占用可用空间,但一定要确保至少有50px的空间。希望这将帮助。
#1
18
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.
元素名指向占据大部分空间的控件。当然,这只可能与元素有关,因为元素的宽度是有限的。如果你对一个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,它接受一个对象[],解析它到float,并返回最大值(如果您自己使用它,并且不需要处理转换器的不良使用,那么它只需要一个linq查询)
This way also supports dynamic changing of the MinWidth
.
这种方式也支持动态变化的最小宽度。
#2
12
Set Width = "Auto"
in XAML.
设置宽度= XAML中的“Auto”。
Then in the code:
然后在代码:
MinWidth = ActualWidth
Width = new GridLength(1, GridUnitType.Star)
#3
2
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中遇到了正确大小网格列的问题。有几样东西我试过了,但后来我找到了均格线。这是我的终极解决方案。它只是工作。我以前不知道……似乎它在VS工具箱中默认不存在(?),因此不知道它是否存在。
You can find more about UniformGrid from here.
您可以从这里找到更多关于均匀网格的信息。
#4
1
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
}
#5
0
Simply you can use below code to set max or min width of the column
只需使用下面的代码设置列的最大或最小宽度
<ColumnDefinition Width="*" MaxWidth="40"/>
This will take the available space but it will limit to max 40px
这将占用可用空间,但将限制为最大40px
you can do the same with minwidth also
你也可以对minwidth进行同样的操作
<ColumnDefinition Width="*" MinWidth="50"/>
This will take the available space but always make sure it has 50px space at least. Hope this will help.
这将占用可用空间,但一定要确保至少有50px的空间。希望这将帮助。