c# - Filter Listview WPF -
i have "attached property" , need generic. in example, work properly, need cast variable of type "employee". possible create more generic, "attached property" more generic, rather casting "employee", converted string , use listview
public static readonly dependencyproperty filtersourceproperty = dependencyproperty.registerattached("filtersource", typeof (textbox), typeof (listviewextension), new frameworkpropertymetadata(null, ontextboxset)); public static textbox getfiltersource(dependencyobject dobj) { return (textbox)dobj.getvalue(filtersourceproperty); } public static void setfiltersource(dependencyobject dobj, textbox value) { dobj.setvalue(filtersourceproperty, value); } private static void ontextboxset(dependencyobject dobj, dependencypropertychangedeventargs e) { var listview = dobj listview; var textbox = e.newvalue textbox; if ((listview != null) && (textbox != null)) { textbox.textchanged += delegate(object sender, textchangedeventargs tcea) { var view = collectionviewsource.getdefaultview(listview.itemssource); if (view == null) return; view.filter += item => { var textfilter = ((textbox)sender).text; var itempl = (employee)item; return itempl.username.contains(textfilter); }; }; } } in .xaml
<listview ...> ... ... tools:listviewextension.filtersource="{binding elementname=txtfilter}" ... ... </listview >
does not require use of textbox in ontextboxset.
try :
public static readonly dependencyproperty filtersourceproperty = dependencyproperty.registerattached("filtersource", typeof(string), typeof(extentions), new frameworkpropertymetadata(null, ontextboxset)); public static string getfiltersource(dependencyobject dobj) { return (string)dobj.getvalue(filtersourceproperty); } public static void setfiltersource(dependencyobject dobj, string value) { dobj.setvalue(filtersourceproperty, value); } private static void ontextboxset(dependencyobject dobj, dependencypropertychangedeventargs e) { var listview = dobj listview; var text = e.newvalue string; if ((listview == null) || (text == null)) return; var view = collectionviewsource.getdefaultview(listview.itemssource); if (view == null) return; view.filter += item => { var itempl = (employee) item; return itempl.username.contains(text); }; } in xaml :(set path of element)
<listview tools:listviewextension.filtersource="{binding elementname=txtfilter,path=text,mode=twoway,updatesourcetrigger=propertychanged}"/> edited :
private static void ontextboxset(dependencyobject dobj, dependencypropertychangedeventargs e) { var listview = dobj listview; var text = e.newvalue string; if ((listview == null) || (text == null)) return; var view = collectionviewsource.getdefaultview(listview.itemssource); if (view == null) return; view.filter += item => { var type = item.gettype(); if (type == typeof(employee)) { var itempl = (employee)item; return itempl.username.contains(text); } if (type == typeof(person)) { var itempl = (person)item; return itempl.name.contains(text); } //and more types .............. return false; }; }
Comments
Post a Comment