簡體   English   中英

當用戶更改對話框的大小時,如何設置wpf文本框以自動調整大小?

[英]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>

或者將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>

TextBox刪除MinHeightMinWidth ,並將HorizonalAlignment更改為Stretch

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

編輯:

如果要在兩個方向(水平和垂直)調整TextBox大小,則必須使用除StackPanel之外的其他容器,以便TextBox大小調整是獨立的。

像這樣的東西:

<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