Implementing Search As You Enter in Silverlight

I am trying to do a search while typing in a Silverlight application. The idea is that I have a screen with a textedit control and a list. The list is filled with all my data.

When a user types something into a text box, the following happens:

  • All elements that do not contain all letters from user input are hidden.
  • The corresponding letters of the elements of the visible list are highlighted in a different color.

I'm not sure how to start with this, so all pointers, samples and tips are welcome!

+3
source share
2 answers

CollectionViewSource. CollectionViewSource . ListBox CollectionViewSource Filter, . " " Text, Filter. "KeyUp" TextBox, , Refresh CollectionViewSource.

CollectionViewSource: http://xamlcoder.com/blog/2010/10/27/filtering-data-using-collectionviewsource/

http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.filter.aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx

http://timheuer.com/blog/archive/2009/11/04/updated-silverlight-3-datagrid-grouping-data-pagedcollectionview.aspx

http://bea.stollnitz.com/blog/?p=392

Sudo:

// ViewModel - properties should fire NotifyPropertyChanged
public class ViewModel : INotifyPropertyChanged
{
  public ViewModel
  {
    this.Data = new CollectionViewSource();
    this.Data.Source = this.GenerateObjects();
    this.Data.Filter += (s,e) =>
    {
      // TODO: add filter logic
      DataObject item = e.Item as DataObject;
      return item.Name.Contains(this.SearchText);
    };
  }
  public string SearchText{get;set;}
  public CollectionViewSource Data {get;set;}

  private List<DataObject> GenerateObjects(){ // generate list of data objects }
}

// View XAML
<StackPanel>
  <TextBox Text="{Binding SearchText, Mode=TwoWay}" KeyUp="OnKeyUp"/>

  <ListBox ItemsSource="{Binding Data.View}"/>
</StackPanel>


// View Code Behind
public class View : UserControl
{
  public View() { this.DataContext = new ViewModel(); }

  private ViewModel ViewModel { get { return this.DataContext as ViewModel; } }

  private OnKeyUp()
  {
    this.ViewModel.Data.View.Refresh();
  }
}
+7

AutocompleteBox Silverlight Toolkit.

, , , , .

+3

Source: https://habr.com/ru/post/1786462/


All Articles