gpt4 book ai didi

wpf - 使 Viewbox 垂直缩放但水平拉伸(stretch)

转载 作者:行者123 更新时间:2023-12-03 14:50:49 25 4
gpt4 key购买 nike

我想制作一个仅缩放其高度,然后水平拉伸(stretch)其内容的 Viewbox(或类似的东西)。
如果我这样做:

<Viewbox>
<StackPanel>
<Button>Foo</Button>
<Button>Bar</Button>
</StackPanel>
</Viewbox>
然后我得到这个:

(来源: excastle.com)
它就像两个按钮都有 Horizo​​ntalAlignment="Center",然后缩放结果。但我不想要 Horizo​​ntalAlignment="Center";我想要 Horizo​​ntalAlignment="Stretch",像这样:

(来源: excastle.com)
所以我希望它读取其内容所需的高度,仅根据高度计算缩放因子,然后允许缩放的内容水平拉伸(stretch)。
有没有办法使用 Viewbox 和/或某些第三方面板来完成此操作?

最佳答案

WPF 中没有包含这样的控件,但是您可以自己编写一个,而不会太麻烦。这是具有您的规范的自定义 ViewboxPanel:

public class ViewboxPanel : Panel
{
private double scale;

protected override Size MeasureOverride(Size availableSize)
{
double height = 0;
Size unlimitedSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (UIElement child in Children)
{
child.Measure(unlimitedSize);
height += child.DesiredSize.Height;
}
scale = availableSize.Height / height;

return availableSize;
}

protected override Size ArrangeOverride(Size finalSize)
{
Transform scaleTransform = new ScaleTransform(scale, scale);
double height = 0;
foreach (UIElement child in Children)
{
child.RenderTransform = scaleTransform;
child.Arrange(new Rect(new Point(0, scale * height), new Size(finalSize.Width / scale, child.DesiredSize.Height)));
height += child.DesiredSize.Height;
}

return finalSize;
}
}

你像这样使用它:
<local:ViewboxPanel>
<Button>Foo</Button>
<Button>Bar</Button>
</local:ViewboxPanel>

它肯定需要一些工作,但这可能会让你开始。

关于wpf - 使 Viewbox 垂直缩放但水平拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com