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

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

问题描述

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



标签和自定义单选按钮的行为与我预期的相同,但是文本框不会水平拉伸以填充viewbox(对不起,因为rep而无法发布图片)。如果输入内容,文本框将水平填充视图框。



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

 < 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>
<视框边距=0,0,0.917,0>
<标签内容=名字:/>
< / Viewbox>
< Viewbox Grid.Column =1>
< TextBox TextWrapping =Wrap/>
< / Viewbox>
< / Grid>

我已经尝试在视图框中放置网格,堆叠面板和dockpanel(使用LastChildFill =True)然后将这些文本框放在这些布局控件中,但这也不起作用。无论如何,让文本框水平填充父视图框?

这个问题与此类似: WPF TextBox不会填充StackPanel ,但这个问题是与stackpanels,而不是viewboxes。

解决方案

我认为你想要的东西不容易。 ViewBox告诉它的孩子他们有足够的空间,通过布局的措施。之后,显示器将被放入视图框中。现在一个文本框被告知有无限的空间,显然无法伸展。
我有两种解决方案,我认为这不是您想要的,但无论如何都可能有帮助。

第一种:



 < Viewbox Grid.Column =1Grid.Row =0Stretch =Uniform> 
< Grid Width ={Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type Viewbox}},Path = ActualWidth}>
< TextBox TextWrapping =WrapHorizo​​ntalAlignment =StretchVerticalAlignment =Stretch/>
< / Grid>
< / Viewbox>

这会拉伸你的文本框infact,但禁用视图框中预期的行为。为什么?我们告诉ViewBox保持宽高比,并设置文本框来填充视框的整个宽度,这会保持文本框的大小。

第二部分: / p>

可以为网格添加一个高度,该高度取自标签,并使用其视框的缩放转换形式进行修改。



这一个我没有尝试,但它会涉及一个值转换器。



结论:有由于视框布置其子项的方式并不是简单的方法来实现你想要的。如果你只想水平拉伸,我的第一个解决方案工作正常,如果你想扩展。我的猜测是你必须自己做。


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>

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?

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

解决方案

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.

The first:

<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>

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.

The second:

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天全站免登陆