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

Popular posts from this blog

python - How to add an model instance to a django queryset? -

Using Android Volley to post an array to PHP -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -