4

InlineにフォーカスするにはどうすればよいRichTextBoxですか? Text-File から a
を作成し、それを my にロードし、Button_click に応じて 次々とマークします (を再作成します)FlowDocumentrichTextBox1InlineFlowDocument

このコードで:

            richTextBox1.SelectAll();
            richTextBox1.Selection.Text = "";

            string text = System.IO.File.ReadAllText(file);
            int iZeile = 0;

            string[] split = text.Split(new string[] {"\r\n"},StringSplitOptions.None);

                    foreach (string s in split)
                    {
                        if (iZeile != 27)
                        {
                            paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
                        }
                        else
                        {
                            Run run = new Run(split[27]); // adds line with marking
                            run.Background = Brushes.Yellow;
                            paragraph.Inlines.Add(run);
                            paragraph.Inlines.Add("\r\n");
                        }
                        iZeile++;
                    }

            FlowDocument document = new FlowDocument(paragraph);
            richTextBox1.Document = new FlowDocument();
            richTextBox1.Document = document;
            Keyboard.Focus(richTextBox1);
        }

私はそれが..完璧ではないことを知っています。

問題

これまでのところ動作しますが、発生する問題は、MarketInlineが View に入らないことです。これをViewに持ち込む簡単な方法はありInlineますか?

4

2 に答える 2

5

簡単な解決策のように見えましたがFrameworkContentElement.BringIntoView()、以下のコードに入れた後、最初は効果がありませんでした。結局のところ、これはタイミングの問題の 1 つであり (WinForms で同様の問題を見たことがあります)、未処理の Windows メッセージを処理することで解決できます。WPF に直接相当するものはありませんDoEvents()が、よく知られている代替手段が存在します。

これを ButtonClick に配置し、 でマークされた変更//**:

        Paragraph paragraph = new Paragraph();
        Inline selected = null;   //**

        richTextBox1.SelectAll();
        richTextBox1.Selection.Text = "";

        string text = System.IO.File.ReadAllText(@"..\..\MainWindow.xaml.cs");
        int iZeile = 0;

        string[] split = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

        foreach (string s in split)
        {
            if (iZeile != 27)
            {
                paragraph.Inlines.Add(s + "\r\n"); // adds line added without marking
            }
            else
            {
                Run run = new Run(split[27]); // adds line with marking
                run.Background = Brushes.Yellow;
                paragraph.Inlines.Add(run);
                paragraph.Inlines.Add("\r\n");
                selected = run;                // ** remember this element
            }
            iZeile++;
        }

        FlowDocument document = new FlowDocument(paragraph);
        richTextBox1.Document = new FlowDocument();
        richTextBox1.Document = document;
        Keyboard.Focus(richTextBox1);

        DoEvents();                   // ** this is required, probably a bug
        selected.BringIntoView();     // ** 

そしてヘルパーメソッドは、ここから:

    public static void DoEvents()
    {
        Application.Current.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Background, 
            new Action(delegate { }));
    }
于 2013-03-18T15:41:51.213 に答える
3

この方法のいずれかを試す必要があります

richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();

.

richTextBox.AppendText(text);  
richTextBox.ScrollToEnd();

さらなる情報はここここにあります

編集

WPF RichTextBox を少し掘り下げた後、クラウドでオフセットを取得しようとすると、この回答richTextBox.ScrollToVerticalOffset(Offset) を使用できるかもしれません

編集2

OK、さらに調査した後、この実際の例をダウンロードできるリンクをたどって見つけました

于 2013-03-18T08:08:11.383 に答える