c# - Code behind doesn't work as XAML -


i'm learning wpf , have troubles it. made xaml:

<window.resources>   <datatemplate x:key="templatetest">     <button margin="10"             borderthickness="2"             content="{binding path=text}">       <button.effect>         <dropshadoweffect blurradius="20" />       </button.effect>     </button>   </datatemplate> </window.resources>  <stackpanel x:name="stackpanel">   <textbox x:name="textbox"            margin="10">textbox</textbox>    <contentcontrol content="{binding elementname=textbox, path=.}"                   contenttemplate="{staticresource resourcekey=templatetest}" /> </stackpanel> 

and code behind:

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         var resource = this.resources["templatetest"] datatemplate;          stackpanel.children.add(             new contentcontrol()             {                 content = new binding()                 {                     elementname = "textbox",                     path = new propertypath(".")                 },                  contenttemplate = resource,             });     } } 

picture

my problem textbox's text appear in xaml defined control. how make work in code behind too?

you're setting contentcontrol.content binding, not same binding content property value.

to bind property in code behind, need syntax this:

var newcontrol new contentcontrol(); newcontrol.contenttemplate = resource;  binding b = new binding(); b.elementname = "textbox"; b.path = new propertypath(".");  mycontentcontrol.setbinding(contentcontrol.contentproperty, b); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -