c# - How to get the element in the control template in Silverlight? -
i want element in control template code behind.
in xaml below want border element "btnborder" , change color red other color code behind.
<window x:class="wpfapplication4.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"> <grid> <stackpanel name ="st" margin="0,3,0,3" grid.row="4" orientation="horizontal"> <button name="btn" height="22" margin="0,0,25,0"> <button.template x:uid="dd"> <controltemplate x:name="tmp" targettype="button"> <border x:name="btnborder" background="red"> <contentpresenter verticalalignment="center" horizontalalignment="center" /> </border> </controltemplate> </button.template> test button </button> </stackpanel> </grid> </window>
i have tried various methods
gettemplatechild("btnborder") object o = template.findname("btnborder", this.btn);
but these methods returning null.
please let me know doing wrong or correct way access template child code behind?
you can set borderbrush
of button
, bind borderbrush
of border
control in controltemplate.
so when set borderbrush
button
code behind reflect underlying binding , on border
control in controltemplate
.
<page.resources> <style x:key="mybuttonstyle" targettype="button"> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border x:name="btnborder" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" background="{templatebinding background}"> <contentpresenter verticalalignment="center" horizontalalignment="center"/> </border> </controltemplate> </setter.value> </setter> </style> </page.resources>
this how can apply style
<stackpanel> <button style={staticresource mybuttonstyle} name="btn1" content="test button 1" borderbrush="red"/> <button style={staticresource mybuttonstyle} name="btn2" content="test button 2" borderbrush="green"/> </stackpanel>
Comments
Post a Comment