C# - 在ObservableCollection中查找项目

时间:2015-01-02 04:29:09

标签: c# windows-phone-8 filter listbox observablecollection

我正在尝试为列表框添加"搜索" 功能,我已使用绑定该功能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应该只显示我输入的内容。如果我输入的内容不存在,则列表框不应显示项目。 e.g:

enter image description here

我该怎么做并拥有这样的功能?我希望它像Windows Phone应用程序搜索一样。

删除方法(使用上下文菜单):

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

当我点击删除按钮时,它会删除另一个保留我真正要删除的项目。

1 个答案:

答案 0 :(得分:1)

考虑使用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);