2017-07-22 159 views
0

我有一个.NET UWP文本框,有很多文本,我想搜索它中的一个单词。当我点击按钮开始搜索时,它会找到这个词的第一个出现。当我再次点击时,它会发现第二个,如记事本中的ctrl + f)。UWP文本框 - 专注于选择

我想把重点放在找到的世界上,但是当文本足够长以至于有一个滚动条时,它不会将找到的单词放入视图中。

这是在这种状态下屏幕的屏幕抓图,显示了如何调整窗口大小以查看找到的单词。

enter image description here

这里是我的搜索代码(textarea是TextBox类型):

private void Find(string text) 
    { 
     textarea.Focus(FocusState.Programmatic); 
     var start = textarea.SelectionStart + textarea.SelectionLength; 
     var found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase); 
     if (found == -1) 
     { 
      textarea.SelectionStart = 0; 
      found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase); 
      if (found == -1) return; 
     } 
     textarea.SelectionStart = found; 
     textarea.SelectionLength = text.Length; 
    } 

我已经试图把textarea.Focus(FocusState.Programmatic);在方法的结束以及textarea.Focus(FocusState.Pointer);,但是没有帮助。

UPDATE:

我发现它的聚焦准确,但到最后发现字(到位置,在光标之前找到下一个单词),而不是目前发现的字。

enter image description here

所以我需要更新的重点目前SelectionStart,别到最后。有任何想法吗?我已经尝试再次更改SelectionStart,替换文本并更新布局 - 没有任何帮助。

+0

可以使用Richeditbox – lindexi

+1

我想要Textbox。 –

回答

1

你可以做的是测量你的文字的高度,直到索引,并相应地调整文本框的大小。

private static float GetTextHeightUntilIndex(TextBox textBox, int index) 
    { 
     var height = 0; 
     var textBuffer = textBox.Text; 

     // Remove everything after `index` in order to measure its size 
     textBox.Text = textBuffer.Substring(0, index); 
     textBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); 
     var height = textBox.DesiredSize().Height; 

     // Put the full text back 
     textBox.Text = textBuffer; 

     return height; 
    } 

private void Find(string text) 
    { 
     textarea.Focus(FocusState.Programmatic); 
     var start = textarea.SelectionStart + textarea.SelectionLength; 
     var found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase); 
     if (found == -1) 
     { 
      textarea.SelectionStart = 0; 
      found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase); 
      if (found == -1) return; 
     } 
     textarea.SelectionStart = found; 
     textarea.SelectionLength = text.Length; 

     // ------------------- 

     var cursorPosInPx = GetTextHeightUntilIndex(textarea, found); 

     // First method: resize your textbox to the selected word 
     textarea.Height = cursorPosInPx; 

     // Second method: scroll the textbox 
     var grid = (Grid)VisualTreeHelper.GetChild(textarea, 0); 
     for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++) 
     { 
      object obj = VisualTreeHelper.GetChild(grid, i); 
      if (obj is ScrollViewer) 
       ((ScrollViewer)obj).ChangeView(null, cursorPosInPx, null, true); 
     } 
    } 

不过要小心,对于第一种方法,这取决于你的whatlayout是文本框,调整大小的控制可能有不需要的效果或根本没有效果。

+0

它没有帮助,长文本太慢。有什么办法,如何通过滚动来解决它?检测所选单词的位置并滚动到位置manualy? –

+0

@BananaCake我用滚动方法编辑 – Lovy

+0

不,它也没有帮助。我发现了一些有趣的事情,检查我的更新: –