简体   繁体   English

C#WPF RichTextBox滚动到文本?

[英]C# WPF RichTextBox Scroll to text?

I'm working on alittle writing game and I encountered a problem. 我正在从事小写游戏,但遇到了问题。 I have a richtextbox which contains one paragraph with alot of runs in it, and I want it to scroll to some run i have. 我有一个richtextbox,其中包含一个带有很多运行的段落,我希望它滚动到我拥有的某些运行。 I have a List object which contains all the inlines in the RTB, and the Select() Method doesn't work for some reason, maybe because it's a ready-only rtb. 我有一个List对象,其中包含RTB中的所有内联,而Select()方法由于某些原因而无法正常工作,可能是因为它只是现成的rtb。 Any ideas for a way to scroll to selected word? 对滚动到所选单词有任何想法吗? My Code: 我的代码:

    private bool isKey = false;
    private Paragraph p;
    private List<Inline> inlineList;
    private int inlineIndex = 0, wpm = 0, wordIndex = 0, lineIndex = 0;
    private string[] words;
    public MainWindow()
    {
        InitializeComponent();
        p = new Paragraph();
        foreach (string s in words)
        {
            p.Inlines.Add(s);
            p.Inlines.Add(" ");
        }
        WordBox.Document.Blocks.Clear();
        WordBox.Document.Blocks.Add(p);
        inlineList = p.Inlines.ToList();
        inlineList[0].Background = Brushes.LightGray;
        this.Activate();
        InputBox.Focus();
    }
    //The Method I want to put the scrolling feature in:
    private void MoveWord()
    {
        if (inlineIndex + 2 < inlineList.Count)
        {
            inlineList[inlineIndex].Background = Brushes.Transparent;
            inlineIndex += 2;
            inlineList[inlineIndex].Background = Brushes.LightGray;
            WordBox.Selection.Select(inlineList[inlineIndex].ContentStart, inlineList[inlineIndex].ContentEnd);
        }
        else
            MessageBox.Show(wpm.ToString());
    }

For Example: the rtb contains: Hey Hello what's up word schnitzel hey 例如:rtb包含:嘿,您好,单词schnitzel是怎么回事,嘿

And I want it to scroll to the word "hey". 我希望它滚动到单词“嘿”。

I've tried to use Select() method, which didn't work... 我尝试使用Select()方法,该方法不起作用...

Create runs and add runs (not inlines) 创建运行并添加运行(非内联)
Save a reference to the runs (eg a List) 保存对运行的引用(例如列表)
And then just call Runs[x].BringIntoView() 然后只需调用Runs [x] .BringIntoView()
Have not tested on RTB but I have done this with FlowDocument and FlowDocumentViewer 尚未在RTB上进行测试,但是我已经使用FlowDocument和FlowDocumentViewer进行了此操作

BringIntoView 进入视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM