簡體   English   中英

UWP TextBox-專注於選擇

[英]UWP TextBox - Focus on selection

我有一個帶有大量文本的.NET UWP TextBox,我想在其中搜索一個單詞。 當我單擊按鈕開始搜索時,它將發現該詞的第一個出現。 當我再次單擊時,它將找到第二個,如記事本中的ctrl + f。

我想關注發現的世界,但是當文本足夠長而沒有滾動條時,它將不會顯示發現的單詞。

這是此狀態下的屏幕截圖,顯示了我必須如何調整窗口大小才能看到找到的單詞。

在此處輸入圖片說明

這是我的搜索代碼( 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); ,但均無濟於事。

更新:

我發現它的焦點正確,但是指向最后找到的單詞(定位,找到下一個單詞之前的光標在哪里),而不是當前找到的單詞。

在此處輸入圖片說明

因此,我需要將焦點更新為當前的SelectionStart ,而不是最后一個。 有任何想法嗎? 我已經嘗試過再次更改SelectionStart ,替換文本並更新布局-沒有任何幫助。

您可以做的是測量直到索引的文本的高度,並相應地調整文本框的大小。

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);
        }
    }

但是,請注意,對於第一種方法,根據文本框的布局,調整控件的大小可能會產生不良影響或根本沒有影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM