让 TextBox 拉伸以填充 StackPanel 中的宽度 [英] Let TextBox stretch to fill width in StackPanel

查看:23
本文介绍了让 TextBox 拉伸以填充 StackPanel 中的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WinRT C# Metro 应用程序中有一个 StackPanel,我想将它用作标签/值对(TextBlockTextBox) 像这样:

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 的剩余水平空间.这有可能吗?Horizo​​ntalAlignment/Horizo​​ntalContentAlignment 不起作用.

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.

推荐答案

不幸的是,Metro 没有替代方案 (DockPanel).你可以试试 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屋!

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