Horizo​​ntalAlignment =拉伸,最大宽度和向左对齐? [英] HorizontalAlignment=Stretch, MaxWidth, and Left aligned at the same time?

查看:221
本文介绍了Horizo​​ntalAlignment =拉伸,最大宽度和向左对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该很容易,但我很沮丧。在WPF中,我想要一个TextBox可以延伸到其父级的宽度,但只能延伸到最大宽度。问题是我希望它在其父项中保持合理。要使其伸展,您必须使用Horizo​​ntalAlignment = Stretch,但结果将居中。我已经尝试过Horizo​​ntalContentAlignment,但是它似乎没有任何作用。

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.

我如何使这个蓝色文本框随着窗口的大小而增长,

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>

诀窍是什么?

推荐答案

您可以将 Horizo​​ntalAlignment 设置为Left,设置 MaxWidth ,然后绑定宽度到父元素的 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>

这篇关于Horizo​​ntalAlignment =拉伸,最大宽度和向左对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆