为什么 WPF 网格中带 * 的两列大小不同?
使用以下代码,我希望以两个宽度相同的列表框结束,因为它们进入两个列定义,并带有“*”
而不是这样,看起来大小是根据列表框上的文本大小确定的,这是没有意义的因为该文本比 ListBox 小得多,因此 TextBlock 有足够的空间来容纳文本。
<Window x:Class="UnderstandSizing.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Text1longer" Grid.Row="0" Grid.Column="0" x:Name="Test1" />
<TextBlock Text="Text1" Grid.Row="0" Grid.Column="2" />
<ListBox Grid.Row="1" Grid.Column="0" Height="150" />
<ListBox Grid.Row="1" Grid.Column="2" Height="150" />
<TextBlock Grid.Row="2" Grid.ColumnSpan="3" Text="This textblock sets the max width" Width="300" />
</Grid>
</Window>
WPF 自动调整大小功能让我抓狂...有什么想法吗? 谢谢。
编辑:一切都在 VS2008 中完成,以防万一重要。
with the following code I expected to end with two ListBox with the same Width as they are into two columndefinition with With="*"
Instead of this looks like the size is determined from the size of the text up the ListBox which does not make sense as this text is much smaller than the ListBox and thus the TextBlock has enough space to accommodate the text.
<Window x:Class="UnderstandSizing.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Text1longer" Grid.Row="0" Grid.Column="0" x:Name="Test1" />
<TextBlock Text="Text1" Grid.Row="0" Grid.Column="2" />
<ListBox Grid.Row="1" Grid.Column="0" Height="150" />
<ListBox Grid.Row="1" Grid.Column="2" Height="150" />
<TextBlock Grid.Row="2" Grid.ColumnSpan="3" Text="This textblock sets the max width" Width="300" />
</Grid>
</Window>
The WPF automatic sizing feature is driving me mad ... any ideas?
Thanks.
EDIT: Everything done in VS2008, just in case it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这个:
http://www.wpftutorial.net/GridLayout.html
“星号 ( *):
占用尽可能多的可用空间(填充所有自动和固定大小的列后),按比例分配给所有星形大小的列,因此 3*/5* 与 30*/50* 相同。如果网格大小是根据其内容计算的,则星号调整不起作用。”
您的代码中就是这种情况。我怀疑这也是其他人测试它时看起来不错的原因,如果他们将网格粘贴到大于 TextBlock 设置的 300 像素的窗口中。如果我使用完全相同的 XAML,我会遇到与您相同的问题。
编辑:这就是“为什么”。请参阅此问题以获取可能的替代解决方案: Wpf:网格:我如何共享列/行高度宽度?
最近的答案(不是提问者选择的答案)似乎是这种情况下最有用的答案。
Look at this:
http://www.wpftutorial.net/GridLayout.html
"Star (*):
Takes as much space as available (after filling all auto and fixed sized columns), proportionally divided over all star-sized columns. So 3*/5* means the same as 30*/50*. Remember that star-sizing does not work if the grid size is calculated based on its content."
Which is the case in your code. I suspect this also is the reason it looked alright for others testing it, if they pasted the Grid into a Window larger than the 300 pixels set by your TextBlock. I get the same issue you do if I use exactly the same XAML.
Edit: So that's for the "why". See this question for a possible alternative solution: Wpf: Grid: How can i share column/row height width?
The most recent answer (not the one chosen by asker) seems to be the most useful one in this case.
亚历克斯. A找到了发生这种情况的确切原因,并且我幸运地找到了解决方案。只需将 * 更改为 0 我就得到了预期的结果(如果你问我的话,我会觉得很奇怪):
Alex. A found the exact cause of what is happening and I found a solution in a lucky strike. Just changing the * for 0 I get the expected result (weird if you ask me):
对我来说,这在运行时工作得很好。不要相信 GUI 设计者,他们是敌人。
For me this works just fine, at runtime that is. Do not trust GUI designers, they are the enemy.
在设计时和运行时对我有用。
GUI 设计者不应该显示这些像素大小。对我来说,它显示
1*
,这意味着您的屏幕截图来自与您粘贴的代码不同的代码。Works for me at design time and at runtime.
The GUI designer shouldn't be showing those pixel sizes. For me it shows
1*
, which means your screenshot is from code different than you pasted.