【发布时间】:2014-04-13 12:26:05
【问题描述】:
这个问题的标题可能听起来令人困惑,所以我会尽力解释自己。
我正在创建一个应用程序,该应用程序包含许多具有许多 foreign key 约束的表。例如,就我而言,我有一个学生。每个学生表与父母详细信息表和医疗详细信息表都有foreign key 关系。
为了使我的应用程序易于使用,我实现了一系列不同的filters 来帮助用户搜索大量数据。
我有一个listview 来显示学生记录,以及父母和医疗详细信息。但是,我想做的是根据父母详细信息中的一组标准搜索学生记录。例如,搜索父母姓名。例如;是一个学生的父母叫 Bob,listview 将 filter 学生的父母叫 Bob。
这是我尝试过的;
//Constructor;
StudentList = new ObservableCollection<StudentViewModel>(GetStudents());
CollectionViewSource.GetDefaultView(StudentList).Filter = new Predicate<object>(MainFilter);
//Properties
private string contactNameSearch;
public string ContactNameSearch
{
get { return contactNameSearch; }
set
{
contactNameSearch = value;
CollectionViewSource.GetDefaultView(StudentList).Refresh();
OnPropertyChanged("ContactNameSearch");
}
}
private bool FilterContactNameSearch(object obj)
{
StudentContactViewModel item = obj as StudentContactViewModel;
if (item == null) return false;
if (String.IsNullOrWhiteSpace(ContactNameSearch)) return true;
if (ContactNameSearch.Trim().Length == 0) return true;
if (item.Name1.ToLower().Contains(ContactNameSearch.ToLower())) return true;
return false;
}
public bool MainFilter(object o)
{
return FilterContactNameSearch(o); // &... and more filters
}
//Xaml
<TextBox Height="23" Name="txtContactName" Width="100"
Text="{Binding ContactNameSearch, UpdateSourceTrigger=PropertyChanged}"/>
上面的代码 sn-p 是我为我的另一个 filter 实现的,它们都可以正常工作。但是,当我在我的应用程序中 bind 这个 property 到 textbox 时,突然之间填充 listview 的数据消失了。 This link here shows the before and after
如果我在 ContactNameSearch property 周围加上 breakpoint,我还会出现“无窗口源”对话框。
我整理了一个小样本,可以在by click here找到它
因此,我的问题是;我是否正确实施了这一点,如果没有,是否有其他方法可以做到这一点?
【问题讨论】:
-
阅读您在示例中显示的代码,您是否尝试使用“||”进行更改而不是这里的“&”?返回 FilterSchoolSearch(o) & FilterContactNameSearch(o) & FilterNameSearch(o) & FilterFormSearch(o) & FilterIDSearch(o) & FilterEnrollment(o) & FilterNameSearch(o);因为如果你放一个“&”,如果其中一个过滤器返回 false,那么 MainFilter 的所有结果都将为 false...
-
@MauroBilotti 感谢您的回复。我会改变它,看看是否有效。
-
不客气!告诉我它是否能解决您的问题...
-
我应该把它们都改成
|而不是&? -
我认为使用触发事件的按钮然后将 ListView 绑定到过滤后的 ObservableCollection 会更容易。
标签: wpf listview binding filter