【发布时间】:2012-10-03 10:46:50
【问题描述】:
这是一个包含UserControl 的 WPF 窗口。 UserControl 是一个简单的Grid,有 2 列,没有明确的宽度。在第一列中,我添加了一个简单的TextBox 设置来水平拉伸。第二列是空的。
当我缩小窗口的宽度时,WPF 会裁剪我的UserControl,即使它的右侧仍有巨大的空白空间!看起来它试图让我的第二列与我的第一列大小相同,即使它是空的,但奇怪的是,这发生在我的 UserControl 之外(否则空白区域将是粉红色的!)...
MainWindow.XAML:
<Window x:Class="WPF_Grid_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Grid_Test"
Title="MainWindow" Height="350" Width="525">
<local:TestUserControl HorizontalAlignment="Left"/>
</Window>
UserControl.XAML:
<UserControl x:Class="WPF_Grid_Test.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Pink" Height="100">
<Grid Margin="0" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Text="Why is WPF cropping me even though there's a huge blank space to eat into?" VerticalAlignment="Center" />
</Grid>
</UserControl>
我尝试将所有Margins 和Paddings 设置为0,没有变化。
当UserControl 设置为水平居中(而不是左侧)时,行为更加怪异!而不是在右侧创建巨大的边距,边距实际上均匀分布在左边和右边。我希望我的UserControl 的动态调整大小仅在 WPF 用完所有可用空间后发生。这不是任何程序员所期望的吗?
(我的真实项目实际上有 3 列,分别包含一个 TextBlock、一个 TextBox 和一个 Button,但行为是相同的:UIElements 将在 WPF 吃进它自己的自动生成的边距之前被裁剪)
当我在列上设置明确的宽度(即“自动”)时,WPF 将在裁剪我的 UserControl 之前正确删除其空白。但这会阻止 WPF 在窗口变得更薄时缩小 UserControl。
所以我的第一个问题是,这不是 WPF 错误吗?
我的第二个问题,如何设置列,以便 WPF 仅在我的 UserControl 真正空间不足时才开始调整其大小?
这是 .NET 4.0。 here 存在类似的问题,但我不太了解解决方案。
更新:
这是UserControl 的另一个示例代码。当按照建议将列宽设置为“自动”时,UserControl 将在窗口缩小时停止缩小其元素,并最终在空间确实不足时裁剪最右边的元素:
<UserControl x:Class="WPF_Grid_Test.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Pink" Height="100">
<Grid Margin="0" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Text="Resize us" VerticalAlignment="Center" Grid.Column="0" />
<TextBox Text="only when" VerticalAlignment="Center" Grid.Column="1" />
<TextBox Text="you're out of space!" VerticalAlignment="Center" Grid.Column="2" />
</Grid>
</UserControl>
我试图实现的行为是UserControl 在我缩小窗口时保留其DesiredSize。但只有当窗口超出空白区域时,UserControl 才应该接受“缩小”处理。
尝试从 ColumnDefinitions 中删除 Width=Auto,您应该会看到最右边的元素是如何调整大小的,在容器缺少空间之前。这就是为什么我认为这确实是一个 WPF 布局错误。
【问题讨论】:
标签: c# .net wpf grid-layout