2015-01-20 129 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

相关问题