Kar*_*ann 4 c# wpf xaml focus richtextbox
我如何将 a 集中Inline在 a 中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\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);
}
Run Code Online (Sandbox Code Playgroud)
我知道它不......完美。
到目前为止它有效,但出现的问题是我市场Inline没有进入视图。有没有一种简单的方法可以将其Inline带入View?
简单的解决方案似乎是,FrameworkContentElement.BringIntoView()但将其放入下面的代码后,它最初没有任何效果。事实证明,这是这些时间问题之一(我在 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(); // **
Run Code Online (Sandbox Code Playgroud)
和辅助方法,从这里:
public static void DoEvents()
{
Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new Action(delegate { }));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1869 次 |
| 最近记录: |