Вопрос

I have a dictionary app and I have the word's database connected to my application, and now I want to make a simple search box (using a textBox and a Button) to search in my list box for a word.

I found this in the button code which is useful for me, but what about the rest?

private void searchBTN_Click_1(object sender, RoutedEventArgs e)
{
    foreach (Wordst item in mainlist.Items)
    {
        if (item.listwordsw == txtSearch.Text)
        {
            //What should I have Here?
        }

        foreach (Wordst subItem in mainlist.Items)
        {
            if (subItem.Listwords2== txtSearch.Text)
            {
                //What should I have Here?
            }
        }
    }
}
Это было полезно?

Решение

From MSDN

CollectionViewSource is a proxy for a CollectionView class, or a class derived from CollectionView. CollectionViewSource enables XAML code to set the commonly used CollectionView properties, passing these settings to the underlying view. CollectionViewSource has a View property that holds the actual view and a Source property that holds the source collection. You can think of a collection view as the layer on top of the binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself. If the source collection implements the INotifyCollectionChanged interface, the changes raised by the CollectionChanged event are propagated to the views. Because views do not change the underlying source collections, each source collection can have multiple views associated with it. For example, you may have a collection of Task objects. With the use of views, you can display that same data in different ways. For example, on the left side of your page you may want to show tasks sorted by priority, and on the right side, grouped by area.

You want to use CollectionViewSource in order to support Filtering (finding items in ListBox. where lvDictionary is your ListBox

public partial class FilteringSample : Window
{
            public FilteringSample()
            {
                    InitializeComponent();
                    List<Word> items = new List<Word>();
                    items.Add(new User() { Name = "Apple"});
                    items.Add(new User() { Name = "Orange"});
                    items.Add(new User() { Name = "Pineapple" });
                    items.Add(new User() { Name = "Define",});
                    lvDictionary.ItemsSource = items;

                    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvDictionary.ItemsSource);
                    view.Filter = UserFilter;
            }

            private bool UserFilter(object item)
            {
                    if(String.IsNullOrEmpty(txtFilter.Text))
                            return true;
                    else
                            return ((item as Word).Name.IndexOf(txtFilter.Text, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            private void txtFilter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
            {
             CollectionViewSource.GetDefaultView(lvDictionary.ItemsSource).Refresh();
            }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top