wpf 在 XAML 中怎么说:例如 TextBox 的默认宽度和高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/503233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In XAML how to say: default width and height for e.g. TextBox
提问by Edward Tanguay
So I'm coming at WPF from a HTML perspective.
所以我是从 HTML 的角度来看 WPF。
I just want to put a TextBox
on my Window like this:
我只想像这样TextBox
在我的 Window 上放一个:
<Grid>
<TextBox Name="theName" />
</Grid>
Turns out that the TextBox
is then HUGE, covers the whole window. (!)
事实证明,TextBox
然后是巨大的,覆盖了整个窗口。(!)
Ok, that's not what I want, but I don't want to define the EXACT size either since I know Height
and Width
should be flexible, so I try:
好的,这不是我想要的,但我也不想定义 EXACT 大小,因为我知道Height
并且Width
应该灵活,所以我尝试:
<TextBox Name="theName" Width="Auto" Height="Auto"/>
Same thing. So I try:
一样。所以我尝试:
<TextBox Name="theName"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
Same thing. So I just hard code the sizes:
一样。所以我只是硬编码尺寸:
<TextBox Name="theName" Width="100" Height="20"/>
Which I know is not a good programming practice in WPF.
我知道这在 WPF 中不是一个好的编程习惯。
So, what how do you tell TextBox
to "display default sizes for the font size being used"?
那么,如何告诉TextBox
“显示正在使用的字体大小的默认大小”?
采纳答案by Steve Brouillard
You can take Bryan's example even a bit further. By specifying a specific alignment that isn't stretch and further constrain the TextBox so that it won't expand beyond a certain size. eg:
你可以更进一步地举 Bryan 的例子。通过指定不拉伸的特定对齐方式并进一步约束 TextBox,使其不会扩展到特定大小。例如:
<Grid x:Name="LayoutRoot">
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Text="TextBox" TextWrapping="Wrap"
MinWidth="15" MinHeight="20" MaxWidth="500" MaxHeight="50"/>
</Grid>
You can take it even further by setting up rows/columns inside the Grid and constraining them in various fashions. As you're coming from an HTML background, think of it like using a table to control layout. Remember that you can also nest other container objects (i.e. StackPanels, WrapPanels, other Grids, etc...).
您可以通过在 Grid 内设置行/列并以各种方式约束它们来更进一步。由于您来自 HTML 背景,因此可以将其想象为使用表格来控制布局。请记住,您还可以嵌套其他容器对象(即 StackPanels、WrapPanels、其他 Grids 等...)。
The challenge with XAML and the WPF/Silverlight controls is that they a very flexible, so you've got to get a handle on all the options and how they affect layout.
XAML 和 WPF/Silverlight 控件的挑战在于它们非常灵活,因此您必须掌握所有选项以及它们如何影响布局。
Good luck. I'm going through this exact same thing now.
祝你好运。我现在正在经历同样的事情。
回答by Stefan
Use a different container. The Grid always streches its child controls to fill the grid cell.
使用不同的容器。Grid 总是拉伸其子控件以填充网格单元格。
You could use e.g. a stackpanel which only streches its controls in one direction.
您可以使用例如仅在一个方向上拉伸其控件的堆栈面板。
回答by Bryan Anderson
In addition to using a different panel as Stefan mentioned you could just give the TextBox an alignment that isn't Stretch. e.g.
除了使用 Stefan 提到的不同面板之外,您还可以给 TextBox 一个不是 Stretch 的对齐方式。例如
<TextBox Name="theName" HorizontalAlignment="Left" VerticalAlignment="Top"/>
回答by Nir
The sizes in WPF aren't pixels, they are "device independent pixels" that are 1/96 of an inch - so in today's normal DPI setup they map 1:1 to pixels.
WPF 中的大小不是像素,它们是 1/96 英寸的“设备独立像素” - 所以在今天的正常 DPI 设置中,它们将 1:1 映射到像素。
But, if you run the program in high DPI mode the TextBox will grow with the DPI (and the font).
但是,如果您在高 DPI 模式下运行程序,TextBox 将随着 DPI(和字体)而增长。
So setting an hard-coded size isn't that bad.
所以设置一个硬编码的大小并没有那么糟糕。
Other than that you can only use HorizontalAlignment and VerticalAlignment that are not "Stretch", this will size the TextBox to content - but then an empty TextBox will be tiny.
除此之外,您只能使用不是“拉伸”的 HorizontalAlignment 和 VerticalAlignment,这会将 TextBox 的大小调整为内容 - 但是一个空的 TextBox 会很小。
You can set VerticalAlignment to "Center", "Top" or "Bottom" to get automatic height of about one line (maybe backed up by a MinHeight setting to avoid problems really tiny fonts) and then set the Width so the TextBox width does not change as the user types into it.
您可以将 VerticalAlignment 设置为“Center”、“Top”或“Bottom”以获得大约一行的自动高度(可能由 MinHeight 设置支持以避免出现非常小的字体问题),然后设置 Width 以便 TextBox 宽度不会随着用户输入而改变。