HorizontalAlignment=Stretch、MaxWidth 和 Left 同时对齐? [英] HorizontalAlignment=Stretch, MaxWidth, and Left aligned at the same time?
问题描述
这看起来应该很容易,但我很难过.在 WPF 中,我想要一个 TextBox,它可以拉伸到它的父级的宽度,但只能达到最大宽度.问题是我希望它在其父级中保持合理.要使其拉伸,您必须使用 HorizontalAlignment="Stretch",但结果是居中的.我已经尝试过 HorizontalContentAlignment,但它似乎没有做任何事情.
This seems like it should be easy but I'm stumped. In WPF, I'd like a TextBox that stretches to the width of it's parent, but only to a maximum width. The problem is that I want it to be left justified within its parent. To get it to stretch you have to use HorizontalAlignment="Stretch", but then the result is centered. I've experimented with HorizontalContentAlignment, but it doesn't seem to do anything.
如何让这个蓝色文本框随着窗口的大小而增长,最大宽度为 200 像素,并且左对齐?
How do I get this blue text box to grow with the size of the window, have a maximum width of 200 pixels, and be left justified?
<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>
有什么诀窍?
推荐答案
你可以设置 HorizontalAlignment
为 Left,设置你的 MaxWidth
然后绑定 Width
到父元素的 ActualWidth
:
You can set HorizontalAlignment
to Left, set your MaxWidth
and then bind Width
to the ActualWidth
of the parent element:
<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>
这篇关于HorizontalAlignment=Stretch、MaxWidth 和 Left 同时对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!