简体   繁体   English

当用户更改对话框的大小时,如何设置wpf文本框以自动调整大小?

[英]How do I set a wpf text box to resize automatically when the user changes the size of dialog box?

How do I set a wpf text box to resize automatically when the user changes the size of dialog box? 当用户更改对话框的大小时,如何设置wpf文本框以自动调整大小?

 <Window x:Class="MemoPad.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="LightGray" 
    Title="Window1" Height="350" Width="700" >
<StackPanel Orientation="Vertical">
    <Menu DockPanel.Dock ="Right">
        <MenuItem Header="Find" x:Name="gMNuFind" />
    </Menu>
    <Button Content=" Find " 
          Margin="5,10,5,5"
          x:Name="gBuFind" 
          />
    <TextBox Margin="0,0,0,0"
          HorizontalAlignment="Left"
          VerticalAlignment="Top" 
          MinHeight="270" MinWidth="690"                  
          x:Name = "gTBxInfo" 
          TextWrapping="Wrap"
          AcceptsReturn="True"
          ScrollViewer.VerticalScrollBarVisibility="Auto" 
          />
</StackPanel>

Or change StackPanel to Grid 或者将StackPanel更改为Grid

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="LightGray" 
    Title="Window1" Height="350" Width="700" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="Find" x:Name="gMNuFind" />
        </Menu>
        <Button Grid.Row="1" Content=" Find " 
          Margin="5,10,5,5"
          x:Name="gBuFind" 
          />
        <TextBox Grid.Row="2" Margin="0,0,0,0"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch" 
          MinHeight="270" MinWidth="690"                  
          x:Name = "gTBxInfo" 
          TextWrapping="Wrap"
          AcceptsReturn="True"
          ScrollViewer.VerticalScrollBarVisibility="Auto" 
          />
    </Grid>
</Window>

Remove MinHeight and MinWidth from the TextBox , and change HorizonalAlignment to Stretch TextBox删除MinHeightMinWidth ,并将HorizonalAlignment更改为Stretch

<TextBox Margin="0,0,0,0"
  HorizontalAlignment="Stretch"
  VerticalAlignment="Top"               
  x:Name = "gTBxInfo" 
  TextWrapping="Wrap"
  AcceptsReturn="True"
  ScrollViewer.VerticalScrollBarVisibility="Auto" />

Edit: 编辑:

If you want to resize the TextBox in both directions (horizontally and vertically), you would have to use a different container other than StackPanel , so that the TextBox sizing is independent. 如果要在两个方向(水平和垂直)调整TextBox大小,则必须使用除StackPanel之外的其他容器,以便TextBox大小调整是独立的。

Something like this: 像这样的东西:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Menu>
        <MenuItem Header="Find" x:Name="gMNuFind" Grid.Row="0"/>
    </Menu>
    <Button x:Name="gBuFind" 
            Content=" Find " 
            Margin="5,10,5,5"     
            Grid.Row="1"/>
    <TextBox x:Name = "gTBxInfo" 
             Margin="0,0,0,0"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"               
             TextWrapping="Wrap"
             AcceptsReturn="True"
             ScrollViewer.VerticalScrollBarVisibility="Auto" 
             Grid.Row="2"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM