2011-08-17 48 views
1

我想創建一個足以顯示3行文本的WPF文本框。到目前爲止,我有這樣的代碼:顯示一個文本框顯示三行

System.Windows.Controls.TextBox myTextbox = new TextBox() 
       { 
        AcceptsReturn = true, 
        MinLines = 3, 
        MaxLines = 3, 
        TextWrapping = TextWrapping.Wrap, 
        FontFamily = new FontFamily("Microsoft Sans Serif"), 
        FontSize = 11, 
       }; 

然而,當myTextBox被佈局也不能保證它獲得足夠的高度,以顯示3行文字。理想情況下,我想指定FontSize「這麼小,以至於myTextBox有3行文字的空間,並且不會更小」。有沒有辦法做到這一點?

+0

這對我的作品,給我的空間正好3行。它必須是您的佈局中的風格或其他內容導致問題。父母控制是否會限制其高度? –

+0

是的,有一個佈局上下文。當然,我可以嘗試更改上下文而不是文本框。 – user181813

回答

0

好吧,我最終只是把TextBoxScrollViewer,像這樣:

System.Windows.Controls.ScrollViewer myScrollViewer = 
    new ScrollViewer(){Content = myTextbox}; 
// Now place the ScrollViewer where the TextBox was before. 
1

下面是一個基於我上面的評論的插圖。

<Border VerticalAlignment="Top" Height="Auto"> 
    <TextBox FontSize="11" MinLines="3" MaxLines="3" AcceptsReturn="True" FontFamily="Microsoft Sans Serif" VerticalAlignment="Top"/> 
</Border> 

給我三行。

<Border VerticalAlignment="Top" Height="11"> 
    <TextBox FontSize="11" MinLines="3" MaxLines="3" AcceptsReturn="True" FontFamily="Microsoft Sans Serif" VerticalAlignment="Top"/> 
</Border> 

只給我一個。邊框限制了TextBox的高度。