2011-12-05 40 views
0

所以我相信這可能是一個簡單的答案,我只是沒有看到它,但經過20個谷歌搜索和3天試圖找出我的自己,我投降。下面是我使用的xaml。我需要的是兩個文本框填充所有剩餘空間並且大小相同。我嘗試過StackPanel,DockPanel,Grid(如下圖),甚至嵌套每一個並嵌套一個UniformGrid。似乎沒有任何工作,我只是用MinHeight指定的任何高度得到2個文本框。將Grid.Row設置從「自動」更改爲*只會導致文本框居中對齊,並在窗口縮小和增大時移動。無法獲取文本框(2)來填充網格空間

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

請發表您的風格=「{StaticResource的文本框樣式}」 – Paparazzi

回答

3

有兩個問題與代碼發佈

網格的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> 

或設置你的文本框標籤自動高度屬性。在對象的標籤設置屬性將覆蓋一個風格酒店

<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="2" ... /> 
<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="4" ... /> 
+0

正如我所說,我知道這很簡單。我認爲我沒有理解這一點的原因是我單獨嘗試了這些方法,但不是在同一時間。非常感謝你! –

0

編輯:好吧,我想你的代碼,你需要改變的是你的網格RowDefinitions,嘗試

<Grid.RowDefinitions> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition Height="2*"/> 
    <RowDefinition Height="23"/> 
    <RowDefinition Height="2*"/> 
</Grid.RowDefinitions> 

,你可能想要刪除您的藏漢了minHeight。事實上,你應該嘗試去除所有你明確的高度和寬度,讓WPF照顧這一點,那是什麼佈局系統。

+0

呀,了minHeight在那裏,因爲最初它只是一個文本框沒有翻轉按鈕它,我只是從來沒有刪除它。 :)瑞秋的答案不管這個,但謝謝你指出,我討厭在任何地方都沒用。 –

2

這是幾分鐘前由別人回答,但現在的答案已經消失。嘗試刪除這些行:

<Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="VerticalAlignment" Value="Top" /> 

正如所覆蓋的彈力

+0

否,因爲樣式先來並設置左/頂值,然後是本地值。請參閱[ValuePrecendence](http://msdn.microsoft.com/en-us/library/ms743230.aspx#listing)以供參考。 – dowhilefor

+0

只是爲了讓人們知道這不是正確的答案,只會刪除一次,並接受答案。 – Paparazzi

+0

不要刪除它,它仍然是一個很好的參考,讓人們確信它不是這樣。相信我,我在這3天的考驗中多次嘗試過。:) –