如何正确显示文本文件的内容并允许用户进行文本搜索? [英] How may I properly display the content of a text file and allow textsearch to the user?
问题描述
亲爱的社区,
这是我到目前为止所得到的:
我打开了一个文本文件并将其读取为行,保存每行进入一个ObservableCollection< string> ;.
这就是我需要的:
一种在我的ui中显示字符串的方法应用程序,以便可以进行文本搜索。
这是我到目前为止所尝试的:
< U> 1。选项
我使用数据绑定并将ObservableCollection连接到ListView。显示文字有效,但我在搜索时遇到了一些困难。
我有一个文本框,输入了一个文本,一个按钮开始搜索。找到与搜索到的文本匹配的行没有问题,但我无法更新列表视图的选定项目。我尝试通过将c#代码的属性设置为int值而将ListView的SelectedIndex属性绑定到它。
我使用snoop来确保绑定有效,所以它确实如此。但是没有更新ListView。
另一点是:即使选择有效,Listview会滚动到所选项目吗?
我不这么认为,这就是我尝试另一种方式的原因(2.选项)。
我确实实现了INotifyPropertyChanged接口,并尝试设置updatesourcetrigger在xaml。
这是代码:
私人 void 搜索( object param)
{
int counter = 0 ;
foreach ( string line in FixdataFile)
{
if ( - 1 < line.IndexOf( SearchText))
{
SelectedLine = counter;
break ;
}
counter ++;
}
}
public int SelectedLine
{
get { return _selectedLine; }
set {_ selectedLine = value ;通知( SelectedLine); }
}
public ObservableCollection< string> FixdataFile
{
get { return _fixdataFile; }
set {_ fixdataFile = value ; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void 通知( string strPropName)
{
if (PropertyChanged!= null )
{
PropertyChanged( this , new PropertyChangedEventArgs(strPropName));
}
}
< ListView 填充 = 3 ScrollViewer.VerticalScrollBarVisibility = 自动
ItemsSource = {Binding FixdataFile} 已删除 = Gainsboro
SelectedIndex = {Binding SelectedLine,Mode = TwoWay,
UpdateSourceTrigger = PropertyChanged} >
< ListView.ItemTemplate >
< DataTemplate >
< TextBlock 文本 = {Binding} / >
< / DataTemplate >
< / ListView.ItemTemplate >
< / ListView >
2。选项
经过一番研究后,我找到了FlowDocument类及其Wrappers。我阅读了一些教程并尝试了一些实验。
我能够显示文本,但我认为这不是实际的正确方法,因为FlowDocumentPageViewer的搜索功能不起作用。
我这样试过:
< 边框
< FlowDocumentReader >
< FlowDocument >
< 段落 LineHeight = 8 TextIndent = - 16 >
< ItemsControl ItemsSource = < span class =code-keyword> {Binding FixdataFile,Mode = OneWay}
< span class =code-attribute> ItemTemplate = {StaticResource StringCo llection} / >
< /段落 >
< / FlowDocument >
< / FlowDocumentReader >
< / Border >
< Window.Resources >
< DataTemplate x:Key = StringCollection >
< TextBlock 文字 = {Binding Mode = OneWay} TextAlignment = 左 FontSize = 12
FontFamily = Arial / >
< ; / DataTemplate >
< / Window.Resources >
也许有人有想法改进我的代码或用ui显示文本的其他方式授予文本搜索功能。
搜索时我发现了类似于集合视图类的东西,它是一个提供过滤器的集合包装器。但过滤器是不可接受的。我需要突出显示与searchtext相匹配的文字。
我会感谢任何提示或代码片段。
有一个好的。
我想你可以试试
listview.ScrollIntoView(FixdataFile [SelectedLine]);
Dear community,
this is what I got so far:
I opened a textfile and read it line for line, saving each line into an ObservableCollection<string>.
This is what I need:
A way to show the strings in the ui of my application so that a text search is possible.
This is what I tried so far:
1. option
I used databinding and connected the ObservableCollection to a ListView. Showing the text worked but I had some difficulties with the search.
I had a textbox, entered a text and a button started the search. Finding lines that matched the searched text was no problem but I couldn't update the selected item of the listview. I tried that by setting a Property of the c# code to an int value und the ListView's SelectedIndex Property is bound to it.
I used snoop to be sure that the binding works, and so it does. But no update of the ListView happens.
Another point is: Even if the selection worked, will the Listview scroll to the selected item ?
I don't think so, that is the reason I tried another way (2. option).
I did implement the INotifyPropertyChanged Interface and also tried to set the updatesourcetrigger in xaml.
Here's the code:
private void Search(object param)
{
int counter = 0;
foreach(string line in FixdataFile)
{
if(-1 < line.IndexOf(SearchText))
{
SelectedLine = counter;
break;
}
counter++;
}
}
public int SelectedLine
{
get { return _selectedLine; }
set { _selectedLine = value; Notify("SelectedLine"); }
}
public ObservableCollection<string> FixdataFile
{
get { return _fixdataFile; }
set { _fixdataFile = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void Notify(string strPropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(strPropName));
}
}
<ListView Padding="3" ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding FixdataFile}" removed="Gainsboro"
SelectedIndex="{Binding SelectedLine, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
2. option
After some research I found the FlowDocument class and their Wrappers. I read some tutorials and also tried some experimenting.
I was able to show the text but I think it was not the actual correct way, because the search function of the FlowDocumentPageViewer did not work.
I tried it like this:
<Border
<FlowDocumentReader>
<FlowDocument>
<Paragraph LineHeight="8" TextIndent="-16">
<ItemsControl ItemsSource="{Binding FixdataFile, Mode=OneWay}"
ItemTemplate="{StaticResource StringCollection}"/>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
</Border>
<Window.Resources>
<DataTemplate x:Key="StringCollection">
<TextBlock Text="{Binding Mode=OneWay}" TextAlignment="Left" FontSize="12"
FontFamily="Arial"/>
</DataTemplate>
</Window.Resources>
Maybe somebody has an idea to improve my code or another way to show text in ui with granting a text search feature.
While searching I found something like a collectionview class, a wrapper to Collections that offers a filter. But filters are not acceptable. I need something like highlighting text matching the searchtext.
I will be thankful for any hint or code snippets.
Have a good one.
I think you can try
listview.ScrollIntoView(FixdataFile[SelectedLine]);
这篇关于如何正确显示文本文件的内容并允许用户进行文本搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!