2011-01-24 83 views
4

有時,當我使用文本框時,無法一直向下滾動以查看最後一個單詞。Silverlight 4文本框不顯示全部內容

我已經包含一個具有相同內容但寬度不同的三個文本框的示例。左邊的一個不顯示所有的單詞。

選擇它移動到文本框的末尾,然後按'ctrl'+'end'。當我爲左側的文本框('_tb1')執行此操作時,我看不到光標,也看不到最後一個單詞。看起來光標和文字在文本框的「下方」。我可以標記並複製未顯示的文字。最後一個字應該是「si +」,見下面的代碼。我已驗證文本框的「文本」屬性包含所有文本。

迄今爲止,只有當我使用'TextWrapping ='Wrap''和某些寬度時纔會發生這種情況。

有關如何解決它的任何建議?

Textbox to the left does not show last words

<UserControl x:Class="Silverlight4TextBoxProblem.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid Width="500" Height="150"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <TextBox Name="_tb1" Grid.Column="0" TextWrapping="Wrap" 
       FontFamily="Arial" FontSize="12" 
       VerticalScrollBarVisibility="Visible" 
       Width="100"/> 
     <TextBox Name="_tb2" Grid.Column="1" TextWrapping="Wrap" 
       FontFamily="Arial" FontSize="12" 
       VerticalScrollBarVisibility="Visible" 
       Width="75"/> 
     <TextBox Name="_tb3" Grid.Column="2" TextWrapping="Wrap" 
       FontFamily="Arial" FontSize="12" 
       VerticalScrollBarVisibility="Visible" 
       Width="150"/> 
     <Button Grid.Column="3" Click="ButtonClick" Content="Assert _tb1"/> 
    </Grid> 
</UserControl> 

代碼隱藏

public partial class MainPage : UserControl 
{ 
    private readonly string ErrorText = @"Lorem ipsum dolor sit amet+++," + Environment.NewLine 
     + "consectetur adipisicing elit, sed do eiusmod" + Environment.NewLine 
     + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + Environment.NewLine 
     + "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo" + Environment.NewLine 
     + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse" + Environment.NewLine 
     + "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non" + Environment.NewLine 
     + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem" + Environment.NewLine 
     + "ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor" + Environment.NewLine 
     + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis" + Environment.NewLine 
     + "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." + Environment.NewLine 
     + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu" + Environment.NewLine 
     + "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in" + Environment.NewLine 
     + "culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit" + Environment.NewLine 
     + "amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore" + Environment.NewLine 
     + "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation" + Environment.NewLine 
     + "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" + Environment.NewLine 
     + "in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" + Environment.NewLine 
     + "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui" + Environment.NewLine 
     + "officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet," + Environment.NewLine 
     + "consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et" + Environment.NewLine 
     + "dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco" + Environment.NewLine 
     + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in" + Environment.NewLine 
     + "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." + Environment.NewLine 
     + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia" + Environment.NewLine 
     + "deserunt mollit anim id est laborum. Lorem ipsum dolor si+"; 

    public MainPage() 
    { 
     InitializeComponent(); 

     _tb1.Text = ErrorText; 
     _tb2.Text = ErrorText; 
     _tb3.Text = ErrorText; 
    } 

    private void ButtonClick(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(_tb1.Text.Last().ToString()); 
    } 
} 

回答

1

所以我擁有完全相同的標誌。根據你的要求,這可以通過使用RichTextBox來解決。作爲獎勵,它看起來好像你得到更快的滾動。

但是,RTB沒有要綁定的文本屬性或最大長度屬性。我通過繼承RTB到我的CustomTextBox解決了這個問題。

如果掛接到事件contentchanged

public class CustomTextBox : RichTextBox 
{ 
    public CustomTextBox() 
    { 
     ContentChanged += HandleContentChanged; 
    } 
... 

然後

private void HandleContentChanged(object sender, ContentChangedEventArgs e) 
{ 
    var text = GetContent(out blocks); 
    Text = text; 
} 

,並添加TextProperty綁定到

public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new PropertyMetadata(string.Empty, HandleTextChanged)); 

    private static void HandleTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var customTextBox = (CustomTextBox)d; 

     var newValue = (string)e.NewValue; 

     //This method is only run when set from codebehind 
     UpdateOnTextChange(customTextBox, newValue); 
    } 

    private static void UpdateOnTextChange(CustomTextBox customTextBox, string newValue) 
    { 
     int blocks; 

     if (customTextBox.GetContent(out blocks) != newValue) 
     { 
      var binding = customTextBox.GetBindingExpression(TextProperty); 

      customTextBox.Blocks.Clear(); 
      var value = EnsureString(newValue); 
      customTextBox.Selection.Text = value; 

      if (customTextBox.HasTextChanges) 
      { 
       customTextBox.HasTextChanges = false; 
       //reset binding... 
       if (binding != null) 
       { 
        customTextBox.SetBinding(TextProperty, binding.ParentBinding); 
       } 
      } 

      var bindingExpression = customTextBox.GetBindingExpression(TextProperty); 

      if (bindingExpression != null) 
       bindingExpression.UpdateSource(); 
     } 
    } 

而且因爲你會希望能夠以字符串形式獲取CustomTextBox的內容:

public string GetContent(out int blocks) 
    { 
     var builder = new StringBuilder(); 
     blocks = 0; 
     foreach (var run in Blocks.OfType<Paragraph>().Select(paragraph => paragraph.Inlines.FirstOrDefault()).OfType<Run>()) 
     { 
      blocks++; 
      builder.Append(run.Text); 
      builder.Append(Environment.NewLine); 
     } 
     var content = builder.ToString(); 
     if (content.EndsWith(Environment.NewLine, StringComparison.CurrentCulture)) 
      content = content.Substring(0, content.Length - 2); 
     return content ?? string.Empty; 
    } 
1

哇,這真是一個錯誤!看來問題出在TextBoxView內部控件上。它在某些情況下不能正確計算文本。您可以使用Silverlight Spy查看問題的實際來源。

我試圖很難找到問題的解決方法,但找不到一個。我試着用模板搞亂,沒有任何工作100%的時間。

我們應該將此問題報告給Microsoft。

+1

我相信這傢伙有同樣的問題:https://connect.microsoft.com/VisualStudio/feedback/details/549531/silverlight4-textbox-show-multiple-line-text-wrong 我雖然無法重現他的錯誤,但我使用XP和他的Windows 7. – 2011-01-25 09:38:06