【问题标题】:window resize when textbox resize调整文本框大小时调整窗口大小
【发布时间】:2015-05-29 20:35:34
【问题描述】:

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

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


    【解决方案1】:

    在 WPF 中,您通常将TextBox 控件放置在布局Grid 控件中,并将该网格单元格的ColumnDefinition Width 属性设置为某个相对值“*”,因此它将随窗口一起调整大小。不要根据您的示例使用固定的 Width="500":另外,删除 "HorizontalAlignment="Left"(默认值为 HorizontalAlignment="Stretch",因此您可以省略它以简化您的 XAML)。看下面的示例代码sn -p:

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

    注意:同样的技术可以应用于垂直的“高度”属性,以防您需要使其也可调整大小。

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

    【讨论】:

      【解决方案2】:

      HorizontalAlignment设置为Stretch,不要设置Width

      <Grid>
          <TextBox HorizontalAlignment="Stretch"
                   Margin="5,0,0,5"
                   TextWrapping="Wrap"
                   AcceptsReturn="True"
                   Height="70" />
      </Grid>
      

      【讨论】:

        【解决方案3】:

        WPF 中的布局很大程度上依赖于父容器。例如,创建带有标签和输入字段的表单,考虑使用 Grid 面板。默认情况下,WPF 中的控件根据其父级的布局行为调整大小。这是一个带有两个带标签的文本框和两个随窗口调整大小的按钮的窗口示例。

           <Window>
              <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
        
            <Label Content="Contact Name" Grid.Row="0" Grid.Column="0" />
            <TextBox Grid.Row="0" Grid.Column="1" />
        
            <Label Content="Contact Location" Grid.Row="1" Grid.Column="0" />
            <TextBox Grid.Row="1" Grid.Column="1" />
        
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"
                        VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1">
                <Button Content="OK" Width="75" Height="24" Margin="3" />
                <Button Content="Cancel" Width="75" Height="24" Margin="3" />
            </StackPanel>
        
            </Grid>
        </Window>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-28
          • 2019-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-02
          • 2016-09-14
          相关资源
          最近更新 更多