c# - WPF Xaml passing Control in attribute -
i have textbox (called searchbox) , listview (called employeelist). textbox's textchanged event displays search results in listview. works good, need functionality, want capture keyup/down events navigate through listview elements. know can add handler keyup/down events , done, using lot wanted that's reusable.
here's tried do, created static class (called searchboxhelper) , added attachable property. want pass reference listview control (not 1 of it's properties) value attachable dependency property through xaml.
/controls/searchboxhelper.cs
public static class searchboxhelper { public static readonly dependencyproperty helpslistview = dependencyproperty.registerattached("helpslistview", typeof(listview), typeof(searchboxhelper), new propertymetadata(null, onhelpslistviewchanged)); private static void onhelpslistviewchanged(dependencyobject d, dependencypropertychangedeventargs e) { listview listview = d listview; // crashes, because d not of type listview messagebox.show(listview.name); } public static listview gethelpslistview(dependencyobject d) { return d.getvalue(helpslistview) listview; } public static void sethelpslistview(dependencyobject d, listview listview) { d.setvalue(helpslistview, listview); } }
/pages/employeespage.xaml
<control:navpage x:class="dtcinvoicer.pages.employeespage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control="clr-namespace:dtcinvoicer.controls" x:name="page" width="950" height="580" loaded="page_loaded"> <grid margin="10"> <grid.rowdefinitions> <rowdefinition height="40" /> <rowdefinition /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="350" /> <columndefinition width="260" /> </grid.columndefinitions> <stackpanel grid.row="0" grid.column="0"> <textblock fontsize="22" fontweight="semibold" text="employees" /> </stackpanel> <stackpanel grid.row="1" grid.column="0"> <control:polygoncontainer points="0,0 330,0 340,10 350,30 0,30" background="{staticresource gradient_black}"> <wrappanel> <textbox x:name="searchbox" textchanged="searchbox_textchanged" control:searchboxhelper.helpslistview="{x:reference name=employeelist}" margin="5" width="300" height="20" borderthickness="0" background="#30ffffff" foreground="white" /> <image width="18" source="/images/icons/search.png" /> </wrappanel> </control:polygoncontainer> <border height="490" cornerradius="0,0,5,5" background="{staticresource gradient_blue}"> <stackpanel> <control:fxlistview x:name="employeelist" itemdoubleclick="employeelist_itemdoubleclick" height="455" borderthickness="0" background="transparent" itemcontainerstyle="{staticresource fxlistviewitemcontainer_style}" itemtemplate="{staticresource employee_listviewitem_template}" scrollviewer.horizontalscrollbarvisibility="disabled" /> <wrappanel height="30" /> </stackpanel> </border> </stackpanel> <stackpanel margin="10,0,0,0" grid.row="1" grid.column="1"> <control:polygoncontainer points="250,0 20,0 10,10 0,30 250,30" background="{staticresource gradient_black}"> <textblock margin="0,0,5,0" text="open employees" foreground="white" horizontalalignment="right" verticalalignment="center" /> </control:polygoncontainer> <border height="180" cornerradius="0,0,5,5" background="{staticresource gradient_blue}"> <control:fxlistview x:name="openemployeeslist" itemdoubleclick="openemployeeslist_itemdoubleclick" height="160" verticalalignment="top" borderthickness="0" background="transparent" itemcontainerstyle="{staticresource fxlistviewitemcontainer_style}" itemtemplate="{staticresource employeepage_listviewitem_template}" /> </border> </stackpanel> </grid> </control:navpage>
figured out... else might need this..
private static void onhelpslistviewchanged(dependencyobject d, dependencypropertychangedeventargs e) { // listview listview = d listview; // propblem line above, trying convert // dependencyobject listview , it's textbox, should // have been converting new value of dependencyproperty textbox box = d textbox; listview list = e.newvalue listview; if(box == null || list == null) return; box.previewkeydown += delegate(object sender, keyeventargs e2) { if (e2.key == key.down) list.selectedindex += (list.selectedindex + 1 < list.items.count) ? 1 : 0; else if (e2.key == key.up) list.selectedindex -= (list.selectedindex - 1 >= 0) ? 1 : 0; else if (e2.key == key.enter && list.selectedindex >= 0) { // enter key } }; }
Comments
Post a Comment