c# - Working with elements inside a control that is bound to a collection -
i have observablecollection<string>
bound itemscontrol
template button
. content of button 2 textblock
. i'm trying use previewmouserightbuttonup
event of button toggle visibility of 1 of textblocks, without being able use xaml names elements in template i'm hitting wall. there way of getting button's content elements via sender
in preview event, or other way of doing this? related previous question had didn't quite usable answer (probably due explanation, hence simplified example). seems me should happen should make control based off button adds property toggle, thought had in previous question wasn't working. feel property , trigger right way go?
xaml:
<itemscontrol x:name="ic" > <itemscontrol.itemtemplate> <datatemplate> <button previewmouserightbuttonup="button_previewmouserightbuttonup"> <dockpanel> <textblock text="normal" dockpanel.dock="top"/> <textblock text="{binding}" dockpanel.dock="top" visibility="collapsed"/> </dockpanel> </button> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
code behind:
observablecollection<string> x = new observablecollection<string>(); public mainwindow() { x.add("1"); x.add("2"); initializecomponent(); ic.itemssource = x; }
if name hidden text block "secondtextblock", should work:
private void button_previewmouserightbuttonup(object sender, mousebuttoneventargs e) { dockpanel dockpanel = (dockpanel)((button)sender).content; textblock text = (textblock)logicaltreehelper.findlogicalnode(dockpanel, "secondtextblock"); if (text != null) { text.visibility = visibility.visible; } }
regarding comment below: yes, multiple instances of "secondtextblock" created. see snoop screenshot below. these multiple instances ok; not have negative impact.
Comments
Post a Comment