文本框调整大小时窗口调整大小 [英] window resize when textbox resize

查看:70
本文介绍了文本框调整大小时窗口调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的窗口中可用的文本框代码(form1.xaml),我的要求是当我调整窗口大小时,我也想调整文本框的宽度,我怎样才能做到这一点....

Here is the code for Textbox available in my window(form1.xaml),My requirement is when i am resizing my window i want to resize the textbox width also, How can i able to achieve this....

 <TextBox Width="500" HorizontalAlignment="Left" Margin="5,0,0,5" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Result,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" IsEnabled="{Binding OpenMode,Converter={StaticResource EnableModeConverter}}" Height="70" />

推荐答案

在 WPF 中,您通常将 TextBox 控件放置在布局 Grid 控件中并设置 ColumnDefinition Width 该网格单元格的属性为某个相对值*",因此它将随窗口调整大小.不要根据您的示例使用固定的 Width="500":另外,删除 "Horizo​​ntalAlignment="Left"(默认值为 Horizo​​ntalAlignment="Stretch",因此您可以省略它以简化您的 XAML.请参阅以下示例代码片段:

In WPF you typically place TextBox control within layout Grid control and set the ColumnDefinition Width property of that Grid cell to some relative value "*", so it will resize with the Window. Do NOT use a fixed Width="500" as per your sample: also, remove that "HorizontalAlignment="Left" (the default value is HorizontalAlignment="Stretch", so you can just omit it to simplify your XAML). See the following sample code snippet:

<Grid Name="Grid1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="4*"/>
    </Grid.RowDefinitions>

    <TextBox Name="TextBox1" Grid.Row="0" Grid.Column="0" Height="70" Margin="5,0,0,5" TextWrapping="Wrap" AcceptsReturn="True" (...Rest of Your code) />
</Grid>

注意:同样的技术可以应用于垂直的高度"属性,以防你需要调整它的大小.

Note: The same technique could be applied to a vertical "Height" property in case you need to make it also resizable.

希望这会有所帮助.最好的问候,

Hope this will help. Best regards,

这篇关于文本框调整大小时窗口调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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