Problem binding data to controls in dynamically created wpf TabItem -
i having problem putting data controls on wpf tabitem. have defined several datatemplates in xaml. here 1 of them:
<window.resources>
...
<datatemplate x:key="memotab">
<textbox name="memotextbox" horizontalalignment="stretch" verticalalignment="stretch" acceptsreturn="true" />
</datatemplate>
...
</window.resources>
i create new tab in code behind follows:
tabitem tab = new tabitem();
tab.header = "memo";
tab.contenttemplate = (datatemplate)findresource("memotab");
tab.applytemplate();
system.windows.controls.textbox tb = (system.windows.controls.textbox)tab.template.findname("memotextbox", tab);
if (tb != null) tb.datacontext = memo; //string memo created earlier linq query
tabcontrol.items.add(tab); //tabcontrol xaml defined
the problem tb null, , therefore no data appears in text box (the text box shows in tab , functional)
i not use xaml create tabs in tabcontrol (except first one, not using datatemplate , fine) because meant added , removed user @ run time.
any ideas?
you may need contentpresenter
of tabitem
. can reference msdn this.
// getting contentpresenter of tab contentpresenter mycontentpresenter = findvisualchild<contentpresenter>(tab); // finding textbox datatemplate set on contentpresenter datatemplate mydatatemplate = mycontentpresenter.contenttemplate; textbox mytextbox = (textbox)mydatatemplate.findname("memotextbox", mycontentpresenter);
the method findvisualchild...
private childitem findvisualchild<childitem>(dependencyobject obj) childitem : dependencyobject { (int = 0; < visualtreehelper.getchildrencount(obj); i++) { dependencyobject child = visualtreehelper.getchild(obj, i); if (child != null && child childitem) return (childitem)child; else { childitem childofchild = findvisualchild<childitem>(child); if (childofchild != null) return childofchild; } } return null; }
Comments
Post a Comment