Wpf 约束StackPanel';s到最小子元素的宽度

Wpf 约束StackPanel';s到最小子元素的宽度,wpf,layout,stackpanel,Wpf,Layout,Stackpanel,如何将垂直WPFStackPanel的宽度约束到其包含的最窄项。堆栈面板的宽度不得大于任何其他子元素的宽度。您不能。垂直方向的StackPanel始终会根据其子级请求分配尽可能多的宽度 你最好写一个自定义面板来实现你想要的行为。你不能。垂直方向的StackPanel始终会根据其子级请求分配尽可能多的宽度 您最好编写一个自定义面板来实现所需的行为。不幸的是,IValueConverter方法并不总是有效;例如,如果以静态方式将子集合添加到StackPanel,则绑定时子集合将为空(我发现)。最简单

如何将垂直WPF
StackPanel
的宽度约束到其包含的最窄项。
堆栈面板的宽度不得大于任何其他子元素的宽度。

您不能。垂直方向的
StackPanel
始终会根据其子级请求分配尽可能多的宽度


你最好写一个自定义面板来实现你想要的行为。

你不能。垂直方向的
StackPanel
始终会根据其子级请求分配尽可能多的宽度

您最好编写一个自定义面板来实现所需的行为。

不幸的是,IValueConverter方法并不总是有效;例如,如果以静态方式将子集合添加到StackPanel,则绑定时子集合将为空(我发现)。最简单的解决方案是创建自定义面板:

public class ConstrainedStackPanel : StackPanel
{
    public ConstrainedStackPanel()
    {
    }

    protected override Size MeasureOverride(Size constraint)
    {
        foreach (var item in this.Children)
        {
            // FrameworkElement has the Width property we care about.
            FrameworkElement element = item as FrameworkElement;
            if (element != null)
                constraint.Width = Math.Min(element.Width, constraint.Width);
        }

        return base.MeasureOverride(constraint);
    }

    protected override Size ArrangeOverride(Size arrangeSize)
    {
        foreach (var item in this.Children)
        {
            // FrameworkElement has the Width property we care about.
            FrameworkElement element = item as FrameworkElement;
            if (element != null)
                arrangeSize.Width = Math.Min(element.Width, arrangeSize.Width);
        }

        return base.ArrangeOverride(arrangeSize);
    }
}
您可以使用以下XAML所示的面板:

<StackPanel Margin="5">
    <TextBlock Text="StackPanel:" FontWeight="Bold" />
    <StackPanel x:Name="panelA">
        <Button Width="100" Content="100" />
        <Button Width="200" Content="200" />
        <Button Width="300" Content="300" />
        <Button Width="400" Content="400" />
    </StackPanel>
    <TextBlock Text="ConstrainedStackPanel:" FontWeight="Bold" Margin="0,10,0,0" />
    <l:ConstrainedStackPanel x:Name="panelB">
        <Button Width="100" Content="100" />
        <Button Width="200" Content="200" />
        <Button Width="300" Content="300" />
        <Button Width="400" Content="400" />
    </l:ConstrainedStackPanel>
</StackPanel>

将呈现如下内容:

我希望这能有所帮助。

不幸的是,IValueConverter方法并不总是有效;例如,如果以静态方式将子集合添加到StackPanel,则绑定时子集合将为空(我发现)。最简单的解决方案是创建自定义面板:

public class ConstrainedStackPanel : StackPanel
{
    public ConstrainedStackPanel()
    {
    }

    protected override Size MeasureOverride(Size constraint)
    {
        foreach (var item in this.Children)
        {
            // FrameworkElement has the Width property we care about.
            FrameworkElement element = item as FrameworkElement;
            if (element != null)
                constraint.Width = Math.Min(element.Width, constraint.Width);
        }

        return base.MeasureOverride(constraint);
    }

    protected override Size ArrangeOverride(Size arrangeSize)
    {
        foreach (var item in this.Children)
        {
            // FrameworkElement has the Width property we care about.
            FrameworkElement element = item as FrameworkElement;
            if (element != null)
                arrangeSize.Width = Math.Min(element.Width, arrangeSize.Width);
        }

        return base.ArrangeOverride(arrangeSize);
    }
}
您可以使用以下XAML所示的面板:

<StackPanel Margin="5">
    <TextBlock Text="StackPanel:" FontWeight="Bold" />
    <StackPanel x:Name="panelA">
        <Button Width="100" Content="100" />
        <Button Width="200" Content="200" />
        <Button Width="300" Content="300" />
        <Button Width="400" Content="400" />
    </StackPanel>
    <TextBlock Text="ConstrainedStackPanel:" FontWeight="Bold" Margin="0,10,0,0" />
    <l:ConstrainedStackPanel x:Name="panelB">
        <Button Width="100" Content="100" />
        <Button Width="200" Content="200" />
        <Button Width="300" Content="300" />
        <Button Width="400" Content="400" />
    </l:ConstrainedStackPanel>
</StackPanel>

将呈现如下内容:


我希望这会有所帮助。

我已经尝试绑定到ActualWidth属性,甚至创建了一个转换器来抵消该值,这非常有效,但有一个例外:当您扩展容器的大小时,宽度会正确更新,但当要使容器变小时,实际内容的宽度会变小,但“页面宽度”会变小任何滚动浏览者都不会。我确信有办法解决这个问题,但我还没有找到。

我已经尝试绑定到ActualWidth属性,甚至创建了一个转换器来抵消该值,这非常有效,但有一个例外:当您扩展容器的大小时,宽度会正确更新,但是当要使容器变小时,实际内容的宽度会变小,但任何ScrollViewer的“页面宽度”都不会变小。我肯定有办法解决这个问题,但我还没有找到