我在两个不同的堆栈面板中并排放置了两个FlowDocuments。我需要一种方法来同时搜索特定文本的文档。如果我在文本框中键入“car”,两个流文档阅读器都应该搜索并滚动到下一个“car”实例(如果有的话)。有没有办法实现这个目标? FlowDocument位于FlowDocumentReader中。
答案 0 :(得分:3)
这里我有一个基本的WPF XAML布局,你指定了2个FlowDocumentReaders。我有一个搜索TextBox,我会在搜索文本发生变化时运行代码:
<Window x:Class="WpfFlowTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfFlowTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<!-- Search Box -->
<TextBox Name="SearchTextBox" DockPanel.Dock="Top" TextChanged="TextBox_TextChanged"/>
<!-- 2 Flow Readers -->
<UniformGrid Columns="2">
<FlowDocumentReader Name="FlowReader1">
<FlowDocument>
<Paragraph>
Here is some text in panel number 1
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
<FlowDocumentReader Name="FlowReader2">
<FlowDocument>
<Paragraph>
Here is some more text in panel number 2
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
</UniformGrid>
</DockPanel>
</Window>
在MainWindow.xaml.cs文件中,我有这段代码,它将突出显示文本与您输入内容匹配的流文档:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var searchText = SearchTextBox.Text;
DoSearch(FlowReader1, searchText);
DoSearch(FlowReader2, searchText);
}
private void DoSearch(FlowDocumentReader reader, string search)
{
var doc = reader.Document;
var text = doc.ContentStart;
var docRange = new TextRange(doc.ContentStart, doc.ContentEnd);
docRange.ClearAllProperties();
while (true)
{
var next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
var txt = new TextRange(text, next);
int indx = txt.Text.IndexOf(search);
if (indx > 0)
{
var sta = text.GetPositionAtOffset(indx);
var end = text.GetPositionAtOffset(indx + search.Length);
var textR = new TextRange(sta, end);
// Make it yellow
textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
}
text = next;
}
}
答案 1 :(得分:0)
我已经为想要滚动的人发布了我的轻微修改。
private void TextBox_TextChanged(object sender, EventArgs e)
{
var searchText = SearchTextBox.Text;
if (searchText != null || searchText != "")
{
var FlowReader1 = (FlowDocumentReader)diffResults.Children[0];
var FlowReader2 = (FlowDocumentReader)oldResults.Children[0];
DoSearch(FlowReader1, searchText);
DoSearch(FlowReader2, searchText);
}
}
private void DoSearch(FlowDocumentReader reader, string search)
{
bool toScroll = true;
var doc = reader.Document;
var text = doc.ContentStart;
var docRange = new TextRange(doc.ContentStart, doc.ContentEnd);
docRange.ClearAllProperties();
while (true)
{
var next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
var txt = new TextRange(text, next);
int indx = txt.Text.IndexOf(search);
if (indx >= 0)
{
var sta = text.GetPositionAtOffset(indx);
var end = text.GetPositionAtOffset(indx + search.Length);
if (end == null)
{
end = text.GetPositionAtOffset(indx + 1);
}
var textR = new TextRange(sta, end);
if (toScroll && text.Paragraph != null)
{
text.Paragraph.BringIntoView();
toScroll = false;
}
// Make it yellow
textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
}
text = next;
}