我想获取包含searchtext的Item。 Lisview应该在带有movieTitle的列表视图中向我展示包含searchtext的项目(文本)。 我有这个网格:
<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>
我在文本框中输入一个单词,Lisview显示包含Textbox.Text的项目。以下是搜索代码:
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()));
};
我想将一个Textblock添加到Listview Item WrapPanel,它显示Contains Word。我如何得到这个单词,其中包含Textbox.Text?
电影课程:
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<Movie> Movies = new List<Movie>();
答案 0 :(得分:1)
确定第一步是将列表更改为可观察集合,因为列表不会更新您的UI。试试这个:
ObservableCollection<Movie> Movies = new ObservableCollection<Movie>();
现在只需将其添加到您想要的任何事件中。
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);
}