无法获取TextBoxes(2)来填充网格空间
所以我相信这可能是一个简单的答案,我只是没有看到它,但经过20次谷歌搜索和3天试图独自解决问题,我投降了。 下面是我使用的xaml。 我需要的是两个文本框填充所有剩余空间并且大小相同。 我尝试过StackPanel,DockPanel,Grid(如下图),甚至嵌套每一个并嵌套一个UniformGrid。 似乎没有任何工作,我只是用MinHeight指定的任何高度得到2个文本框。 将Grid.Row
设置从Auto更改为*只会导致TextBoxes居中并随着窗口的缩小和增长而移动。
<TabItem Header="Notes" Name="notesTab">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="23"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Select Contact:" Height="28" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" FontStyle="Italic" FontWeight="Bold"></Label>
<ComboBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Right" Width="150" ItemsSource="{Binding Source={StaticResource contactList}}" DataContext="{Binding Source={StaticResource contactList}}" Name="contactSelectionBox"></ComboBox>
<WrapPanel Grid.Row="1" Height="Auto" Grid.ColumnSpan="2" Margin="5">
<WrapPanel.Width>
<Binding ElementName="callNotes" Path="ActualWidth" />
</WrapPanel.Width>
<Label Content="TAD" Width="Auto" Name="tadShortcut" MouseLeftButtonDown="tadShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="VI" Width="Auto" Name="verifyInfoShortcut" MouseLeftButtonDown="verifyInfoShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Not there" Width="Auto" Name="uipShortcut" MouseLeftButtonDown="uipShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="WUSP" Width="Auto" Name="wuspShortcut" MouseLeftButtonDown="wuspShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="FNYD" Width="Auto" Name="fnydShortcut" MouseLeftButtonDown="fnydShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Not Employed" Width="Auto" Name="notEmployedShortcut" MouseLeftButtonDown="notEmployedShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Message" Width="Auto" Name="messageShortcut" MouseLeftButtonDown="messageShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Late Fees" Width="Auto" Name="lateFeesShortcut" MouseLeftButtonDown="lateFeesShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Payoff" Width="Auto" Name="payoffReqShortcut" MouseLeftButtonDown="payoffReqShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="No Commit" Width="Auto" Name="noCommit" MouseLeftButtonDown="noCommit_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="12/12" Width="Auto" Name="twelve12" MouseLeftButtonDown="twelve12_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="No ID" Width="Auto" Name="vmNoID" MouseLeftButtonDown="vmNoID_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Wrong #" Width="Auto" Name="wrongNumber" MouseLeftButtonDown="wrongNumber_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Day Off" Width="Auto" Name="dayOff" MouseLeftButtonDown="dayOff_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="FPD" Width="Auto" Name="firstPaymentDefault" MouseLeftButtonDown="firstPaymentDefault_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Repo Pmnt" Width="Auto" Name="repoPayment" MouseLeftButtonDown="repoPayment_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="CS Xfer" Width="Auto" Name="custServ" MouseLeftButtonDown="custServ_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
<Label Content="Spanish" Width="Auto" Name="spanish" MouseLeftButtonDown="spanish_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
</WrapPanel>
<MyNamespace:WatermarkTextBox Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource TextBox Style}" MinHeight="98" Margin="15,5,15,5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Name="callTwoNotes" TextChanged="callTwoNotes_TextChanged" FontWeight="Bold" IsEnabled="False" ClipToBounds="False"></MyNamespace:WatermarkTextBox>
<Button Grid.Row="3" Grid.ColumnSpan="2" Height="23" Name="flipFlopButton" VerticalAlignment="Center" Width="75" Click="flipFlopButton_Click">
<Image Source="Images/FlipFlop.gif" />
</Button>
<MyNamespace:WatermarkTextBox Grid.Row="4" Grid.ColumnSpan="2" Style="{StaticResource TextBox Style}" MinHeight="98" Margin="15,5,15,5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Name="callNotes" TextChanged="callNotes_TextChanged" FontWeight="Bold"></MyNamespace:WatermarkTextBox>
</Grid>
</TabItem>
TextBoxes上的样式的xaml在下面,仅为了完整。
<Style x:Key="TextBox Style" TargetType="{x:Type MyNamespace:WatermarkTextBox}">
<Setter Property="CharacterCasing" Value="Upper" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Height" Value="23" />
<Setter Property="Width" Value="Auto" />
<Setter Property="SelectAllOnGotFocus" Value="True" />
</Style>
代码发布有两个问题
TextBoxes的网格RowDefintion
被列为Auto
而不是*
。 这意味着行将根据单元格内容的大小进行调整,而不是基于网格的大小。 将其更改为*
因此它的大小将基于可用空间。
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="23"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
另一个问题是您的TextBox
样式正在为您的项目定义Height
。 这意味着它将使TextBox保持在指定的高度,而不是重新调整它的大小以适应可用空间。 您可以删除此样式设置器,也可以将网格中文本框的高度设置为自动。
删除这个setter
<Style x:Key="TextBox Style" TargetType="{x:Type MyNamespace:WatermarkTextBox}">
<Setter Property="Height" Value="23" />
...
</Style>
或者将TextBox标签上的Height属性设置为Auto。 在对象的标记中设置属性将覆盖样式属性
<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="2" ... />
<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="4" ... />
这在几分钟前被其他人回答,但现在答案已经消失。 尝试删除这些行:
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
可能会覆盖Stretch
编辑:好吧,我试过你的代码,你需要改变的是你的网格RowDefinitions,试试
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="23"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
你也可能想要删除你的minheight。 事实上,你应该尝试去除所有你明确的高度和宽度,让WPF照顾这一点,那是什么布局系统。
链接地址: http://www.djcxy.com/p/50069.html上一篇: Cannot get TextBoxes(2) to fill grid space
下一篇: TextBox should occupy available space and not grow when filled with text