2016-12-27 46 views
0

在UWP應用程序中,是否可以使用某些文本的大小和字體來計算文本框需要保留多少文本而不滾動?根據文本大小計算文本框大小,但不包含實際的文本框

這可能對設計整體佈局非常有用 - 根據TextBox的大小,可能會切換到其他配置。

因爲我的應用程序是跨平臺的,所以我將在代碼中完成所有這些工作,而不是在xaml中,它的圖形佈局大腦位於平臺無關的部分。

如果知道大小是不可能的,即使知道它的任何一個尺寸都不錯,如果可能的話。

回答

0

試試這個:

public sealed partial class MainPage 
{ 
    private TextBox _textBox; 
    private TextBlock _textBlock; 

    public MainPage() 
    { 
     InitializeComponent(); 
     Loaded += MainPage_Loaded; 
    } 
    private void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     _textBlock = new TextBlock 
     { 
      Margin = new Thickness(10000, 10000, 0, 0), 
     }; 
     MainGrid.Children.Add(_textBlock); 

     _textBox = new TextBox 
     { 
      Width = _textBlock.ActualWidth + 64, //is for clear button space 
      Height = _textBlock.ActualHeight, 
     }; 
     _textBox.TextChanged += _textBox_TextChanged; 
     MainGrid.Children.Add(_textBox); 
    } 

    private void _textBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     _textBlock.Text = _textBox.Text; 
     _textBox.Width = _textBlock.ActualWidth + 64; 
     _textBox.Height = _textBlock.ActualHeight; 
    } 
} 

它不是完美的解決方案,但可能適合。

你只是在屏幕上的某個地方創建textBlock並按照它的大小。

XAML只有1個網格x:Name =「MainGrid」