gpt4 book ai didi

wpf - Horizo​​ntalAlignment=拉伸(stretch)、最大宽度、左对齐同时?

转载 作者:行者123 更新时间:2023-12-03 04:58:58 24 4
gpt4 key购买 nike

这看起来应该很容易,但我很困惑。在 WPF 中,我想要一个 TextBox 拉伸(stretch)到其父级的宽度,但仅限于最大宽度。问题是我希望它在其父级中保持合理。要使其拉伸(stretch),您必须使用 Horizo​​ntalAlignment="Stretch",但结果会居中。我已经尝试过 Horizo​​ntalContentAlignment,但它似乎没有做任何事情。

如何让这个蓝色文本框随着窗口的大小而增长,最大宽度为 200 像素,并且左对齐?

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox Background="Azure" Text="Hello" HorizontalAlignment="Stretch" MaxWidth="200" />
</StackPanel>
</Page>

有什么技巧吗?

最佳答案

您可以将Horizo​​ntalAlignment设置为Left,设置您的MaxWidth,然后将Width绑定(bind)到ActualWidth父元素:

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Name="Container">
<TextBox Background="Azure"
Width="{Binding ElementName=Container,Path=ActualWidth}"
Text="Hello" HorizontalAlignment="Left" MaxWidth="200" />
</StackPanel>
</Page>

关于wpf - Horizo​​ntalAlignment=拉伸(stretch)、最大宽度、左对齐同时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280331/

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