【问题标题】:WPF creates undesired margin when ColumnDefinitions' widths are dynamic?当 ColumnDefinitions 的宽度是动态的时,WPF 会创建不需要的边距?
【发布时间】: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>

我尝试将所有MarginsPaddings 设置为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


    【解决方案1】:

    您应该明确指定列的宽度。

    例如,如果您希望两列的大小始终相同,请使用Width="*"
    如果您希望两列都尽可能宽,请使用Width="Auto"。这将导致列的大小不同。
    如果您希望一列尽可能宽,另一列使用剩余空间,请在第一列使用Width="Auto",在第二列使用Width="*"

    【讨论】:

    • 设置这些会根据其内容为 UserControl 提供固定宽度,这不是我正在寻找的行为。我对 UserControl 调整自身大小并在空间不足时裁剪其元素感到满意。如果您尝试上面的代码,您会注意到 UserControl 已缩小,即使 Container 确实 有空间显示它。
    • 我已经用你的想法更新了我的初始帖子,并解释了它如何不能提供预期的行为。
    猜你喜欢
    • 2012-03-07
    • 2011-12-14
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多