如标题所述,我有一个文本框,其高度设置为Auto
,允许文本换行并接受返回键按下。
目前,当文本框展开时,它会在其他控件的顶部增长,在本例中为“提交”和“取消”按钮。我想要增长的文本框称为txtAddress
,它的提交和取消按钮应该随着文本框的增长而推开。
以下是我的WPF
<Window x:Class="MyProject.AddContact"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Add Contact" Height="310" Width="378" WindowStartupLocation="CenterScreen">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="txtName" VerticalAlignment="Top" Width="266" Foreground="#FFB3B3B3" Text="Contact Name" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
<Image Height="53" HorizontalAlignment="Left" Margin="284,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="60" Source="/BoardiesSMSServer;component/images/no-contact-image.png" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,71,0,0" Name="txtPhoneNumber" VerticalAlignment="Top" Width="170" Text="Phone Number" Foreground="#FFB3B3B3" VerticalContentAlignment="Center" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="188,71,0,0" Name="comboBox1" VerticalAlignment="Top" Width="90">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Work Fax" />
<ComboBoxItem Content="Home Fax" />
<ComboBoxItem Content="Pager" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Custom" />
<ComboBoxItem Content="Callback" />
<ComboBoxItem Content="Car" />
<ComboBoxItem Content="Company Main" />
<ComboBoxItem Content="ISDN" />
<ComboBoxItem Content="Main" />
<ComboBoxItem Content="Other Fax" />
<ComboBoxItem Content="Radio" />
<ComboBoxItem Content="Telex" />
<ComboBoxItem Content="TTY TTD" />
<ComboBoxItem Content="Work Mobile" />
<ComboBoxItem Content="Work Pager" />
<ComboBoxItem Content="Assistant" />
<ComboBoxItem Content="MMS" />
</ComboBox>
<Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="76,226,0,0" Name="btnSubmit" VerticalAlignment="Top" Width="75" Style="{StaticResource RoundCorner}" Click="btnSubmit_Click" />
<Label Content="Phone" Height="28" HorizontalAlignment="Left" Margin="12,37,0,0" Name="label3" VerticalAlignment="Top" Width="266" BorderThickness="0,0,0,1" BorderBrush="#FFA70000" Foreground="#FFA70000" FontWeight="Bold" VerticalContentAlignment="Bottom" />
<Label BorderBrush="#FFA70000" BorderThickness="0,0,0,1" Content="Email" FontWeight="Bold" Foreground="#FFA70000" Height="28" HorizontalAlignment="Left" Margin="12,100,0,0" Name="label1" VerticalAlignment="Top" VerticalContentAlignment="Bottom" Width="266" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,134,0,0" Name="txtEmail" VerticalAlignment="Top" Width="170" Foreground="#FFB3B3B3" Text="Email" />
<ComboBox Height="23" Margin="188,134,0,0" Name="comboBox2" VerticalAlignment="Top" HorizontalAlignment="Left" Width="90">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Mobile" />
<ComboBoxItem Content="Custom" />
</ComboBox>
<Label BorderBrush="#FFA70000" BorderThickness="0,0,0,1" Content="Address" FontWeight="Bold" Foreground="#FFA70000" Height="28" HorizontalAlignment="Left" Margin="12,163,0,0" Name="label2" VerticalAlignment="Top" VerticalContentAlignment="Bottom" Width="266" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="188,197,0,0" Name="comboBox3" VerticalAlignment="Top" Width="90">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Custom" />
</ComboBox>
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="157,226,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" Style="{StaticResource RoundCorner}" Click="btnCancel_Click" />
<TextBox Height="Auto" HorizontalAlignment="Left" Margin="12,197,0,0" Name="txtAddress" VerticalAlignment="Top" Width="170" TextWrapping="WrapWithOverflow" AcceptsReturn="True" MinHeight="23" Foreground="#FFB3B3B3" Text="Address" />
</Grid>
</Window>
感谢您提供的任何帮助。
答案 0 :(得分:1)
您没有充分利用现有的工具。您没有为网格定义任何行和列 - 您只是将所有内容放在一个单元格中,并使用边距来推动周围的事物。如果你对抗WPF的布局系统 - 如果你实际上使用硬编码的静态布局 - 那么是的,你的布局将不会是动态的。
更好的方法是将网格用作网格。添加RowDefinitions,并确保扩展的TextBox与Height =“Auto”在一行中。实际上,对于你正在做的事情,大多数行可能应该是Height =“Auto”,除非你底部有一些东西要填充剩余的空间。
以此为出发点。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Margin="5" />
<TextBox Grid.Row="1" Margin="5" />
<TextBox Grid.Row="2" Margin="5" />
<TextBox Grid.Row="3" Margin="5" AcceptsReturn="True" />
<Button Grid.Row="4" Margin="5" Content="Button" />
</Grid>
或者,如果您发现每个行都使用Height =“Auto”,那么StackPanel是等效的,但标记更简单:
<StackPanel Margin="5">
<TextBox Margin="5" />
<TextBox Margin="5" />
<TextBox Margin="5" />
<TextBox Margin="5" AcceptsReturn="True" />
<Button Margin="5" Content="Button" />
</StackPanel>
答案 1 :(得分:0)
我设法找到一种方法,有很多试验和错误,不确定我是否在谷歌中插入了错误的关键词,但我真的很难追求。
我没有将所有内容都放在网格中,而是使用堆栈面板作为主根,这包含了对于同一行上的组件的其他stackpanel的元素需求。以下是我现在定义的XAML的方法
<Window x:Class="MyProject.AddContact"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="522" MaxHeight="450">
<Window.Resources>
<Storyboard x:Key="showVisibleAnimation">
<ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation BeginTime="0:0:0:0" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.2" />
</Storyboard>
<Storyboard x:Key="hideVisibleAnimation">
<ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation BeginTime="0:0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.2" />
</Storyboard>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
<StackPanel Height="Auto" Margin="5,5,118,5">
<TextBox HorizontalAlignment="Left" Name="txtContactName" Height="23" Text="Contact Name" Margin="0, 0, 4, 4" Width="253" Foreground="#FFB3B3B3" />
<Button HorizontalAlignment="Left" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Foreground="#d1d1d1" Name="btnAddCompany" Height="23" Content="Add Organisation" Click="btnAddCompany_Click" Margin="0, 0, 10, 4" Width="253" />
<TextBox HorizontalAlignment="Left" Foreground="#FFB3B3B3" Name="txtCompany" Text="Organisation" Visibility="Collapsed" Height="23" Margin="0, 0, 4, 4" Width="253"/>
<TextBox HorizontalAlignment="Left" Foreground="#FFB3B3B3" Name="txtTitle" Text="Title" Visibility="Collapsed" Height="23" Margin="0, 0, 4, 4" Width="253"/>
<Label Content="Phone" Height="28" HorizontalAlignment="Left" Width="347" Padding="0,0,4,10" VerticalContentAlignment="Bottom" BorderThickness="0,0,0,1" BorderBrush="#FFA70000" Foreground="#FFA70000" FontWeight="Bold" Margin="0,0,4,4" />
<StackPanel Orientation="Horizontal">
<TextBox HorizontalAlignment="Left" Name="txtPhoneNumber" Text="Phone Number" Height="23" Margin="0, 0, 4, 4" Width="253" Foreground="#FFB3B3B3" />
<ComboBox Height="23" HorizontalAlignment="Left" Name="cboPhoneType" Width="90" Margin="0,0,4,4">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Work Fax" />
<ComboBoxItem Content="Home Fax" />
<ComboBoxItem Content="Pager" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Custom" />
<ComboBoxItem Content="Callback" />
<ComboBoxItem Content="Car" />
<ComboBoxItem Content="Company Main" />
<ComboBoxItem Content="ISDN" />
<ComboBoxItem Content="Main" />
<ComboBoxItem Content="Other Fax" />
<ComboBoxItem Content="Radio" />
<ComboBoxItem Content="Telex" />
<ComboBoxItem Content="TTY TTD" />
<ComboBoxItem Content="Work Mobile" />
<ComboBoxItem Content="Work Pager" />
<ComboBoxItem Content="Assistant" />
<ComboBoxItem Content="MMS" />
</ComboBox>
</StackPanel>
<Label Content="Email" Height="28" HorizontalAlignment="Left" Width="347" Padding="0,0,4,10" VerticalContentAlignment="Bottom" BorderThickness="0,0,0,1" BorderBrush="#FFA70000" Foreground="#FFA70000" FontWeight="Bold" Margin="0,0,4,4" />
<StackPanel Orientation="Horizontal">
<TextBox HorizontalAlignment="Left" Name="txtEmail" Text="Email" Height="23" Margin="0,0,4,4" Width="253" Foreground="#FFB3B3B3" />
<ComboBox Height="23" Name="cboEmailType" HorizontalAlignment="Left" Width="90" Margin="0,0,4,4">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Mobile" />
<ComboBoxItem Content="Custom" />
</ComboBox>
</StackPanel>
<Label Content="Address" Height="28" HorizontalAlignment="Left" Width="347" Padding="0,0,4,10" VerticalContentAlignment="Bottom" BorderThickness="0,0,0,1" BorderBrush="#FFA70000" Foreground="#FFA70000" FontWeight="Bold" Margin="0,0,4,4" />
<StackPanel Orientation="Horizontal">
<TextBox Name="txtAddress" Text="Address" MinHeight="23" Height="Auto" TextWrapping="WrapWithOverflow" AcceptsReturn="True" Width="253" Margin="0,0,4,4" Foreground="#FFB3B3B3" />
<ComboBox Height="23" HorizontalAlignment="Left" Name="cboAddressType" Width="90" Margin="0,0,4,4">
<ComboBoxItem Content="Home" IsSelected="True" />
<ComboBoxItem Content="Work" />
<ComboBoxItem Content="Other" />
<ComboBoxItem Content="Custom" />
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Width="169">
<Button Name="btnSubmit" Click="btnSubmit_Click" Content="Submit" Height="23" Width="75" Margin="5, 5, 5, 5" />
<Button Name="btnCancel" Click="btnCancel_Click" Content="Cancel" Height="23" Width="75" Margin="5, 5, 5, 5" />
</StackPanel>
</StackPanel>
</ScrollViewer>
<Image Height="73" HorizontalAlignment="Left" Margin="371,5,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="104" Source="file:///C:/Users/Chris/Documents/Visual%20Studio%202010/Projects/GUITesti%20g/GUITesti%20g/Images/no-contact-image.png" />
</Grid>
</Window>