簡體   English   中英

C#-在ObservableCollection中查找項目

[英]C# - Find items in ObservableCollection

我正在嘗試為我的Listbox添加一個“搜索”功能,我已經使用ObservableCollection綁定了該功能,但是我不知道該怎么做。

對於我的ObservableCollection

ObservableCollection<ItemProperties> ItemCollection { get; set; }
public class ItemProperties : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ItemProperties() { }

        private string m_ID;
        public string ID
        {
            get { return m_ID; }
            set
            {
                m_ID = value;
                OnPropertyChanged("ID");
            }
        }

        private string m_Title;
        public string Title
        {
            get { return m_Title; }
            set
            {
                m_Title = value;
                OnPropertyChanged("Title");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    }

我將項目加載到列表框

        string[] fileNames = isf.GetDirectoryNames("Files/*.*");
        ItemCollection = new ObservableCollection<ItemProperties>();
        foreach (var Directory in fileNames)
        {
            // code which reads and loads the text files to string which then is added to the Collection
        }
        ItemCollection.Add(new ItemProperties { ID = a_ID, Title = a_Title});
        IEnumerable<ItemProperties> query = ItemCollection.OrderBy(Dat => Dat.Title);
        listBox1.ItemsSource = query;

現在,我有一個啟用TextBox的按鈕。 啟用TextBox並輸入時,listBox1應該僅顯示我輸入的內容。 如果我鍵入的內容不存在,則列表框不應顯示項目。 例如:

在此處輸入圖片說明

我該怎么做並具有這樣的功能? 我希望它像Windows Phone應用程序搜索一樣。

刪除方法(使用上下文菜單):

 var contextMenuOpenedIndex = listBox1.Items.IndexOf((sender as MenuItem).DataContext);
 ItemCollection.RemoveAt(contextMenuOpenedIndex);

當我單擊刪除按鈕時,它將刪除另一項,保留我真正要刪除的項。

考慮將CollectionViewSource用作數據源,而不是直接使用ObservableCollection。 您可以將該對象聲明為XAML元素,也可以在后面的代碼中為其標注尺寸。 每當遇到適當的UI事件(例如,搜索框失去焦點或按下某個鍵)時,只要符合您期望的UI響應性,就刷新視圖。

private CollectionViewSource MySource { get; set; }

private void PopulateView()
{
    string[] fileNames = isf.GetDirectoryNames("Files/*.*");
    ItemCollection = new ObservableCollection<ItemProperties>();
    foreach (var Directory in fileNames)
    {
        // code which reads and loads the text files to string which then is added to the Collection
    }
    ItemCollection.Add(new ItemProperties { ID = a_ID, Title = a_Title});

    // Create view
    MySource = new CollectionViewSource {
        Source = ItemCollection
    };

    // Add sorting support
    MySource.View.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending));

    // Create a filter method
    MySource.View.Filter = obj => 
    {
        var item = obj as ItemProperties;

        // Predicate to determine if search box criteria met; change as needed
        return item.Title.Contains(txtMyFilter.Text);
    }

    // Initialize selected item to avoid SelectionChanged event
    MySource.View.MoveCurrentToFirst()

    // Set as ListBox source
    listBox1.ItemsSource = MySource.View;
}

// Bind to XAML TextBox element's KeyUp event or similar
private void OnFilterKeyUp(object sender, KeyEventArgs e)
{
    MySource.View.Refresh();

    // Include any other display logic here, such as possibly scrolling to top of ListBox
}

關於您的刪除代碼,我不鼓勵您嘗試排列索引。 請嘗試:

ItemCollection.Remove((sender as MenuItem).DataContext as ItemProperties);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM