【发布时间】:2015-01-03 06:44:00
【问题描述】:
我正在尝试为我使用 ObservableCollection 绑定到的 Listbox 添加一个“搜索” 功能,但我没有不知道我该怎么做。
对于我的 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);
当我点击删除按钮时,它会删除另一个项目,保留我真正想要删除的项目。
【问题讨论】:
-
在我的脑海中,
TextBox我认为有一个类似于“Changed”的事件,每次键入时都会触发。在那里,您可以按框中的内容过滤完整列表。这应该会在您键入时更改。
标签: c# windows-phone-8 filter listbox observablecollection