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

查看:762
本文介绍了让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的剩余水平空间.这有可能吗? 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屋!

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