文本框不会拉伸以填充视图框 [英] Textbox will not stretch to fill viewbox

查看:17
本文介绍了文本框不会拉伸以填充视图框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 LOB 表单中的标签和文本框的字体大小随着窗口大小或分辨率的变化而增大和缩小.为了实现这一点,我将标签和文本框放在了视图框中.

I want the font size of my labels and textboxes in my LOB form to grow and shrink with window resize or resolution change. To achieve this I've placed my labels and textboxes within viewboxes.

标签和自定义单选按钮的行为符合我的预期,但文本框不会水平拉伸以填充视图框(抱歉,由于代表无法发布图像).如果您输入文本框,它们将水平填充视图框.

The labels and custom radio buttons behave as I expect, but the textboxes will not stretch horizontally to fill the viewbox (sorry can't post image because of rep). The textboxes will horizontally fill the viewbox if you type into them.

这是我正在使用的代码示例:

Here is an example of the code which I am working with:

  <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.186*"/>
            <ColumnDefinition Width="0.814*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.127*"/>
            <RowDefinition Height="0.873*"/>
        </Grid.RowDefinitions>
        <Viewbox Margin="0,0,0.917,0">
            <Label Content="First name:"/>
        </Viewbox>
        <Viewbox Grid.Column="1">
            <TextBox TextWrapping="Wrap"/>
        </Viewbox>
    </Grid>

我尝试在视图框中放置网格、堆栈面板和停靠面板(LastChildFill="True"),然后将文本框放置在这些布局控件中,但这也不起作用.反正有没有让文本框水平填充父视图框?

I've tried placing grid, stackpanel and dockpanel (with LastChildFill="True") within the viewbox and then placing the textboxes within these layout controls but this didn't work either. Is there anyway to get the textboxes to horizontally fill the parent viewbox?

这个问题与此类似:WPF TextBox 不会填充 StackPanel 但这个问题出在堆栈面板上,而不是视图框上.

This problem is similar to this: WPF TextBox won't fill in StackPanel but this problem is with stackpanels, not viewboxes.

推荐答案

我认为你想要的东西并不容易.ViewBox 在布局的测量过程中告诉它的孩子他们有无限的空间.之后,显示适合视图框.现在一个文本框,被告知有无限的空间,显然不能伸展.我有 2 个解决方案,我认为这不是您想要的,但无论如何可能会有所帮助.

I think what you want is not easily possible. ViewBox tells its children that they have inifite space, in the measure pass of the layouting. After that, the display is fit into the viewbox. Now a textbox, told to have infinite space, obviously can't stretch. I have 2 solutions, which i think are not what you want, but might be helpful anyway.

第一个:

<Viewbox Grid.Column="1" Grid.Row="0" Stretch="Uniform" >
    <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Viewbox}}, Path=ActualWidth}">
      <TextBox TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Viewbox>

这实际上会拉伸您的文本框,但会禁用视图框的预期行为.为什么?我们告诉 ViewBox 保持纵横比,并将文本框设置为填充整个视图框的宽度,从而保持文本框的大小.

this will stretch you textbox infact, but disable the behaviour expected from the viewbox. Why? We told the ViewBox to keep the aspect ratio, and set the textbox to fill the whole width of the viewbox, which keeps the size of the textbox.

第二个:

将向网格添加一个高度,取自标签,并使用其视图框的 scaletransform 进行修改.

would be to add a height to the grid, taken from the label, modified with the scaletransform of its viewbox.

这个我没试过,但它会涉及一个值转换器.

This one i haven't tried, but it would involve a value converter.

结论:没有简单的方法可以实现您想要的,因为视图框布局其子项的方式.如果您只想水平拉伸,如果您想缩放,我的第一个解决方案可以正常工作.我的猜测是你必须自己做.

In conclusion: There is no easy way to achieve what you want, because of the way the viewbox layouts its children. If you just want to stretch horizontally, my first solution works fine, if you want to scale. My guess is you have to do it yourself.

这篇关于文本框不会拉伸以填充视图框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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