让TextBox拉伸以填充StackPanel中的宽度 [英] Let TextBox stretch to fill width in StackPanel
问题描述
我的WinRT C#Metro应用程序中有一个StackPanel
,我想用作标签/值对(TextBlock
和TextBox
)的容器,如下所示:
I have a StackPanel
in my WinRT C# Metro app that I want to use as container for a label/value pair (TextBlock
and TextBox
) like this:
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Label" Width="150" />
<TextBox Text="Value" />
</StackPanel>
现在,我要让TextBox自动填充StackPanel的剩余水平空间.这有可能吗? HorizontalAlignment
/HorizontalContentAlignment
不起作用.
Now what I want is to let the TextBox automatically fill the remaining horizontal space of the StackPanel. Is this possible in any way? HorizontalAlignment
/HorizontalContentAlignment
don't work.
我知道一种替代方法是为此定义一个Grid
.问题是我多次构造此结构,并想在Style
定义中使用它.我不想用x的行和列来定义Grid的定义...
I know that an alternative would be to define a Grid
for that. The problem is that I have this construct several times and want to use it in a Style
definition. I don't want to define the Grid's definition with rows and columns x times...
也许替代方法是定义一个自定义用户控件,但我希望可以轻松扩展TextBox
.
Perhaps the alternative would be to define a custom user control, but I hoped there would be an easy possibility to get the TextBox
to stretch.
推荐答案
不幸的是,替代(DockPanel)不适用于Metro.您可以尝试使用WrapGrid,但我不知道它是否可以解决您的问题(我从未使用过).
Unfortunately the alternative (DockPanel) isnt available for Metro. You could try a WrapGrid, but I dont know if it'll solve your problem (Ive never used it).
做到这一点的唯一真实方法是使用您所描述的网格:
The only real way of doing this is using a Grid as you described:
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Label" Width="150" />
<TextBox Text="Value" Grid.Column="1" />
</Grid>
这篇关于让TextBox拉伸以填充StackPanel中的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!