xaml - How to access Generated WPF controls after Data Binding -
xaml - How to access Generated WPF controls after Data Binding -
please consider next xaml code:
<listbox name="listbox1" itemssource="{binding}" > <listbox.itemtemplate> <datatemplate> <border name="border1"> <textblock text="{binding}" /> </border> </datatemplate> </listbox.itemtemplate> </listbox>
and assign simple array it:
listbox1.datacontext = new[] { "a", "b", "c" };
now question how can access generated objects border (or textblock instances)?
it not possible "border1". not exist.listbox1.itemcontainergenerator.containerfromindex(0)
returns listboxitem content of listboxitem of type string. findname("border1")
returns null update: expect find 3 instances of border (and 3 textblocks, 1 in each border).
once listboxitem, need walk visual tree find looking for.
dr wpf has great articles here
here code article search descendant of particular type
public static visual getdescendantbytype(visual element, type type) { if (element.gettype() == type) homecoming element; visual foundelement = null; if (element frameworkelement) (element frameworkelement).applytemplate(); (int = 0; < visualtreehelper.getchildrencount(element); i++) { visual visual = visualtreehelper.getchild(element, i) visual; foundelement = getdescendantbytype(visual, type); if (foundelement != null) break; } homecoming foundelement; }
wpf xaml binding datatemplate
Comments
Post a Comment