c# - Access textbox from ControlTemplate in UserControl.Resources -
i have control template defined this:
<usercontrol.resources> <controltemplate targettype="{x:type datagrid}" x:key="inputitemscontroltemplate"> <grid> <grid.columndefinitions> <columndefinition width="120"/> <columndefinition width="200"/> <columndefinition width="20"/> <columndefinition width="200"/> </grid.columndefinitions> <grid grid.column="0"> <grid.rowdefinitions> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> </grid.rowdefinitions> <label grid.row="1" content="{dynamicresource ucodestr}" horizontalalignment="left" verticalalignment="bottom" height="27" /> <textbox name="txtucode" grid.row="2" height="23" horizontalalignment="left" verticalalignment="bottom" width="100" text="{binding ucode, updatesourcetrigger=propertychanged}" /> </grid> <grid grid.column="1"> <grid.rowdefinitions> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> </grid.rowdefinitions> <label grid.row="1" content="{dynamicresource goodstr}" horizontalalignment="left" verticalalignment="bottom" height="27"/> <combobox grid.row="2" height="23" width="189" horizontalalignment="left" name="cbgoods" itemssource="{binding path=goods}" selecteditem="{binding path= good, updatesourcetrigger=propertychanged}" displaymemberpath="name" iseditable="true" istextsearchenabled="true" textsearch.textpath="name" /> <label grid.row="3" content="{dynamicresource inputpricestr}" horizontalalignment="left" name="lblinputprice" verticalalignment="bottom" height="27"/> <textbox name="txtinputprice" grid.row="4" textalignment="right" height="23" width="189" horizontalalignment="left" verticalalignment="bottom" text="{binding path= inputprice, updatesourcetrigger=propertychanged, validatesondataerrors=true, notifyonvalidationerror=true, stringformat='n2'}" /> </grid> <grid grid.column="3"> <grid.rowdefinitions> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> <rowdefinition height="25" /> </grid.rowdefinitions> <label grid.row="1" content="{dynamicresource ammountstr}" horizontalalignment="left" name="lblammount" verticalalignment="bottom" height="27"/> <textbox name="txtammount" textalignment="right" grid.row="2" height="23" width="189" horizontalalignment="left" verticalalignment="bottom" text="{binding path=amount, validatesondataerrors=true, notifyonvalidationerror=true, updatesourcetrigger=propertychanged, stringformat='n2'}" /> <label grid.row="3" content="{dynamicresource suggestedpricestr}" horizontalalignment="left" name="lblsuggestedprice" verticalalignment="bottom" height="27"/> <textbox name="txtsuggestedprice" grid.row="4" textalignment="right" height="23" width="189" horizontalalignment="left" verticalalignment="bottom" text="{binding path= suggestedprice, validatesondataerrors=true, notifyonvalidationerror=true, updatesourcetrigger=propertychanged, stringformat='n2'}" /> <checkbox grid.row="5" name="cbhasvatdeduction" ischecked="{binding path=hasvatdeduction}" /> </grid> </grid> </controltemplate> </usercontrol.resources>
i implement template datagrid in codebehind. want access textbox "txtucode" , set focus it. tried this:
inputdocitemsdatagrid.template = (controltemplate)this.findresource("inputitemscontroltemplate"); textbox txtucode = (textbox)inputdocitemsdatagrid.template.findname("txtucode", inputdocitemsdatagrid); txtucode.focus();
but txtucode allways null. how this?
principle, code good. when assigning template this:
inputdocitemsdatagrid.template = (controltemplate)this.findresource("inputitemscontroltemplate");
access elements available after control has loaded, i.e. when element laid out, rendered, , ready interaction.
so try example:
xaml
<window x:class="sampledatagrid.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525" loaded="window_loaded" contentrendered="window_contentrendered"> <window.resources> <controltemplate x:key="inputitemscontroltemplate" targettype="{x:type datagrid}"> <grid> .... </grid> </controltemplate> </window.resources> <grid> <datagrid name="inputdocitemsdatagrid" /> </grid> </window>
code-behind
private void window_loaded(object sender, routedeventargs e) { inputdocitemsdatagrid.template = (controltemplate)this.findresource("inputitemscontroltemplate"); } private void window_contentrendered(object sender, eventargs e) { textbox txtucode = (textbox)inputdocitemsdatagrid.template.findname("txtucode", inputdocitemsdatagrid); txtucode.focus(); }
first, calls window_loaded
event, window_contentrendered
.
or this:
xaml
<grid> <datagrid name="inputdocitemsdatagrid" loaded="inputdocitemsdatagrid_loaded" /> <button content="test" horizontalalignment="left" verticalalignment="bottom" click="button_click" /> </grid>
code-behind
private void inputdocitemsdatagrid_loaded(object sender, routedeventargs e) { inputdocitemsdatagrid.template = (controltemplate)this.findresource("inputitemscontroltemplate"); } private void button_click(object sender, routedeventargs e) { textbox txtucode = (textbox)inputdocitemsdatagrid.template.findname("txtucode", inputdocitemsdatagrid); txtucode.focus(); }
Comments
Post a Comment