C# - 在 ObservableCollection 中查找项目

C# - Find items in ObservableCollection

我正在尝试为我的 Listbox 添加一个 "Search" 功能,我使用 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 并且在我键入时,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);