c# - WPF change text property on TextBlock in ControlTemplate -


i have tabcontrol , have plan generate dynamicaly tabitems , fill controltemplate.

controltemplate:

<window.resources>      <controltemplate x:key="qtemp" targettype="control">         <controltemplate.resources>             <controltemplate x:key="yesbutton" targettype="{x:type button}">                 <grid>                     <image visibility="visible" name="normal" source="/tabitemtemplate;component/images/yes.png" />                     <image visibility="hidden" name="pressed" source="/tabitemtemplate;component/images/yes_p.png" />                 </grid>                 <controltemplate.triggers>                     <trigger property="button.ispressed" value="true">                         <setter targetname="normal" property="visibility" value="hidden"/>                         <setter targetname="pressed" property="visibility" value="visible"/>                     </trigger>                 </controltemplate.triggers>             </controltemplate>             <controltemplate x:key="nobutton" targettype="{x:type button}">                 <grid>                     <image visibility="visible" name="normal" source="/tabitemtemplate;component/images/no.png" />                     <image visibility="hidden" name="pressed" source="/tabitemtemplate;component/images/no_p.png" />                 </grid>                 <controltemplate.triggers>                     <trigger property="ispressed" value="true">                         <setter targetname="normal" property="visibility" value="hidden"/>                         <setter targetname="pressed" property="visibility" value="visible"/>                     </trigger>                 </controltemplate.triggers>             </controltemplate>             <controltemplate x:key="circlebutton">                 <grid>                     <ellipse>                         <ellipse.fill>                             <imagebrush imagesource="{binding path=tag, relativesource={relativesource templatedparent}}" />                         </ellipse.fill>                     </ellipse>                 </grid>             </controltemplate>         </controltemplate.resources>             <grid>                      <grid.rowdefinitions>                         <rowdefinition height="80"/>                         <rowdefinition/>                         <rowdefinition height="auto"/>                     </grid.rowdefinitions>             <ellipse grid.row="1" margin="-1024,-75,0,0" name="progress_bar" fill="#ff01325e" width="424" height="424" strokethickness="0" rendertransformorigin=".5,.5">                 <ellipse.clip>                     <rectanglegeometry rect="0,0,212,424"/>                 </ellipse.clip>                 <ellipse.rendertransform>                     <rotatetransform angle="45"/>                 </ellipse.rendertransform>             </ellipse>             <grid grid.rowspan="3" grid.row="0" width="auto" height="auto" >                 <grid.background>                     <imagebrush imagesource="/tabitemtemplate;component/images/q_bg.png" />                 </grid.background>             </grid>             <textblock grid.row="1" name="qbody" width="400" margin="-120,-100,0,0" foreground="#ff333333" textwrapping="wrap" fontfamily="arial" fontsize="28" textalignment="left" fontweight="bold" verticalalignment="center" text="{binding}" />             <stackpanel grid.row="1" width="350" horizontalalignment="right">                 <button  width="172" height="172"  name="bt_yes" template="{staticresource yesbutton}" click="bt_nextq" />                 <button  width="172" height="172"  name="bt_no" margin="0,20,0,0" template="{staticresource nobutton}" click="bt_nextq" />             </stackpanel>             <image grid.row="2" source="/tabitemtemplate;component/images/toolbar.png" />             <button grid.row="2" width="56" height="56" tag="/tabitemtemplate;component/images/home.png"  horizontalalignment="left" margin="90,0,0,15" template="{staticresource circlebutton}" />             <button grid.row="2" width="56" height="56" tag="/tabitemtemplate;component/images/back.png"  horizontalalignment="left" margin="26,0,0,15" template="{staticresource circlebutton}" />         </grid>     </controltemplate> </window.resources> 

in xaml:

   <tabcontrol horizontalalignment="stretch" name="navtab" verticalalignment="stretch" borderthickness="0" padding="0">         <tabitem name="tabitem1a" header="static">              <control template="{staticresource qtemp}" />         </tabitem>     </tabcontrol> 

cs file:

private void window_loaded(object sender, routedeventargs e)   { (int = 1; < 6; i++) {         tabitem newtab = new tabitem();         control newcontrol = new control();         newcontrol.template = (controltemplate)findresource("qtemp");          //textblock tb = (textblock)newcontrol.template.findname("qbody", newcontrol);         //tb.text = "test" + i.tostring();         newtab.header = "dynamic"+i.tostring();         newtab.name = "question" + i.tostring();         newtab.content = newcontrol;         navtab.items.add(newtab);  } } 

in template have textblock "qbody" , ellipse "progress_bar". im trying add text "qbody" , roratetransform angle "progress_bar" cant access controls in template.

i tried:

textblock tb = (textblock)newcontrol.template.findname("qbody", newcontrol);

but returns null.

can me it. dont know im doing wrong. other way set these 2 controls.

you have marked mvvm, doing violates mvvm principles. progress bar , textbox should bound viewmodel, can access thier properties there.

your controls null because have not been created yet.

but if want use code behind need access controls in apply template event of framework element (tab control)

framework applytemplate event

cheers

edit 1:

you have implement inotifypropertychanged ui made "aware" of changes properties in viewmodel. raise event every iteration of property counter.

also, scope of need cannot covered in 1 answer. recommend reading more mvvm , wpf , maybe use existing framework mvvm-light or caliburn.micro.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -