如何将RichTextBox子内联内联到视图中 [英] How to bring Inline from a RichTextBox Child into View

查看:118
本文介绍了如何将RichTextBox子内联内联到视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 RichTextBox 中集中内联

创建一个 FlowDocument 从文本文件中加载到我的 richTextBox1
中并标记一个 Inline 在Button_click之后(重新创建 FlowDocument

用这段代码:



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

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

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

foreach(split中的字符串)
{
if(iZeile!= 27)
{
paragraph.Inlines.Add(s +\\ \\r\\\
); //添加行,不添加标记
}
else
{
Run run = new Run(split [27]); //添加标记
的行run.Background = Brushes.Yellow;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add(\r\\\
);
}
iZeile ++;
}

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

我知道它不是完美的。

问题



它到目前为止工作正常,但是问题出在我市场内联不进入视图。有没有一种简单的方法可以将 Inline 带入视图?

解决方案

直接的解决方案似乎是 FrameworkContentElement.BringIntoView(),但在将其放入下面的代码后,它最初没有任何效果。事实证明,这是这些计时问题之一(我见过类似的WinForms问题),可以通过处理未完成的Windows消息来解决。 WPF没有直接等价于 DoEvents(),但是存在一个众所周知的替代品。



ButtonClick,用 // ** 标记的更改:

 段落段落=新的段落(); 
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\\\
},StringSplitOptions.None);

foreach(split中的字符串)
{
if(iZeile!= 27)
{
paragraph.Inlines.Add(s +\\ \\r\\\
); //添加行,不添加标记
}
else
{
Run run = new Run(split [27]); //添加标记
的行run.Background = Brushes.Yellow;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add(\r\\\
);
selected = run; // **记住这个元素
}
iZeile ++;
}

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

DoEvents(); // **这是必需的,可能是一个bug
selected.BringIntoView(); // **

以及帮助程序方法,从这里

pre $ public static void DoEvents()
{
Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new Action(delegate {}));
}


How can i focus a Inline in a RichTextBox?
I Create a FlowDocument from a Text-File and load it in my richTextBox1 and mark one Inline after an other accordingly to a Button_click (be recreating the FlowDocument)

with this code:

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

I know its not.. perfect.

the Issue

It works so far but the problem that occurs is me Market Inline doesn't comes intoView. Is there a easy way to bring this Inline intoView?

解决方案

The straightforward solution seemed to be FrameworkContentElement.BringIntoView() but after putting it in the code below it initially had no effect. As it turns out this is one of these timing issues (I've seen similar problems in WinForms) that can be solved by processing the outstanding Windows Messages. WPF has no direct equivalent of DoEvents() but there exists a well known substitute.

I placed this in a ButtonClick, changes marked with //**:

        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();     // ** 

And the helper method, from here:

    public static void DoEvents()
    {
        Application.Current.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Background, 
            new Action(delegate { }));
    }

这篇关于如何将RichTextBox子内联内联到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆