[英]Cannot get TextBoxes(2) to fill grid space
因此,我堅信這可能是一個簡單的答案,但我只是沒有看到它,但是經過20次Google搜索和3天的嘗試,我自己投降了。 我正在使用的xaml在下面。 我需要的是兩個文本框填充所有剩余空間並保持大小相等。 我已經嘗試過StackPanel,DockPanel,Grid(如下所示),甚至嵌套了其中的每一個並嵌套了UniformGrid。 似乎沒有任何效果,我只得到2個文本框,它們具有為MinHeight指定的高度。 將Grid.Row
設置從“自動”更改為*只會導致文本框居中並隨着窗口的縮小和增長而移動。
<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>
發布的代碼有兩個問題
RowDefintion
的網格的RowDefintion
列為Auto
而不是*
。 這意味着將根據單元格內容的大小而不是網格的大小來確定行的大小。 將此更改為*
以便其大小取決於可用空間。
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="23"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
另一個問題是您的TextBox
樣式正在為項目定義Height
。 這意味着它將文本框保持在指定的高度,而不調整其大小以適合可用空間。 您可以刪除此樣式設置器,也可以將“網格”中“文本框”上的“高度”設置為“自動”。
刪除此二傳手
<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" />
可能會覆蓋拉伸
編輯:好的,我嘗試了您的代碼,您需要更改的是您的Grid RowDefinitions,嘗試
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="23"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
並且您可能還希望刪除自己的身高。 實際上,您應該嘗試刪除所有顯式的高度和寬度,讓WPF負責,這就是布局系統的作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.