2015-01-20 68 views
6

當用戶更改對話框的大小時,如何設置wpf文本框以自動調整大小?當用戶更改對話框的大小時,如何設置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> 

回答

3

或更改的StackPanel網格

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

「1」爲最後一個RowDefinition Height做了什麼? – ttom 2015-01-21 19:38:35

+0

看這裏http://www.wpf-tutorial.com/panels/grid-rows-and-columns/ 在行/列定義中,您可以使用固定大小,自動或比率因子(1 *) – rraszewski 2015-01-21 20:03:10

+0

對我來說是新的東西。謝謝! – ttom 2015-01-21 20:32:40

3

TextBox刪除MinHeightMinWidth,並改變HorizonalAlignmentStretch

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

或者只是完全忽略'Horizo​​ntalAlignment'。 – 2015-01-20 21:26:14

+0

默認伸展嗎? – 2015-01-20 21:26:44

+1

我複製/粘貼他的代碼,它的工作原理,如果你只是離開它。查看['FrameworkElement'類](http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/FrameworkElement.cs,e5073af851450ff6),它的初始值似乎是'Horizo​​ntalAlignment.Stretch'。 – 2015-01-20 21:27:12