Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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));
    }
}


XML
<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:
XML
<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.
Posted

1 solution

I think you can try
C#
listview.ScrollIntoView(FixdataFile[SelectedLine]);
 
Share this answer
 
Comments
ChriDo61 1-Dec-15 4:39am    
Hello marktubu,

Thank you for that hint. I was not familiar with that Property of the ListView-class.

I was able to get the wanted behaviour using your suggested code. :)

But since I'm working with MVVM I'm not 100% happy with this solution. I don't want the Viewmodel to have to know the name of any control in the view.
But you gave me a good starting point for a research on that issue. Thanks a lot.
marktubu 1-Dec-15 6:31am    
It's a pity that it didn't help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900