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.