moxmlb
moxmlb

Reputation: 1330

How to Select the Item which contains the Textbox. Search from Textbox to ListView

I want to get the Item which contains the searchtext. The Lisview should show me the item (text), which contains the searchtext, in the listview with the movieTitle. i have this Grid:

<Grid x:Name="GridSearchMovie" Grid.Row="2" Visibility="Hidden" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="txtSearchMovie" TextChanged="txtSearch_TextChanged"/>
        <ListView x:Name="lvSearchMovie" Grid.Row="1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding movieTitle}"/>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Height" Value="50"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>

I type in the Textbox an word and the Lisview Shows me the items which contains the Textbox.Text. Here is the Code for the search:

lvSearchMovie.ItemsSource = Movies;

            TextBox t = (TextBox)sender;
            string Filter = txtSearchMovie.Text;
            ICollectionView cv = CollectionViewSource.GetDefaultView(lvMovies.ItemsSource);
            cv.Filter = o =>
                {
                    Movie m = o as Movie;
                    return (m.movieTitle.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieDirector.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieActor.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieGenre.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieTime.ToString().ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieDate.ToString().ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieStudio.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieFSK.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieRating.ToUpper().Contains(Filter.ToUpper()) ||
                        m.movieSeenTo.ToUpper().Contains(Filter.ToUpper()));
                };

I want to add an Textblock to the Listview Item WrapPanel, which shows the Contains Word. How I get these word, which contains the Textbox.Text?

Movie Class:

public class Movie
{
    public int movieID;
    public string moviePicture;
    public string movieTitle {get; set;}
    public string movieDirector;
    public string movieActor;
    public string movieGenre;
    public string movieTime;
    public string movieDate;
    public string movieStudio;
    public string movieFSK;
    public string movieRating;
 }

List Movies:

List<Movie> Movies = new List<Movie>();

Upvotes: 1

Views: 155

Answers (1)

Dark Templar
Dark Templar

Reputation: 1155

ok first step would be to change your list to an observable collection as lists will not update your UI. try this:

ObservableCollection<Movie> Movies = new ObservableCollection<Movie>();

now just add this to any event you want.

            foreach (var item in cv)
            {
              if (cv.Any(x => m.movieTitle.ToUpper() == Filter.ToUpper()  )
                 { item.movieTitle = txtSearchMovie.Text;}
              if (cv.Any(x => m.movieDirector.ToUpper() == Filter.ToUpper()  )
                 { item.movieDirector = txtSearchMovie.Text;}
              if (cv.Any(x => m.movieActor.ToUpper() == Filter.ToUpper()  )
                 { item.movieActor = txtSearchMovie.Text;}
              if (cv.Any(x => m.movieGenre.ToUpper() == Filter.ToUpper()  )
                 { item.movieGenre = txtSearchMovie.Text;}
              Movies.Add(item);
            }

Upvotes: 1

Related Questions