Wednesday, 15 July 2015

wpf - Can I bind the visibility of a button to the IsSelected property of its containing ItemTemplate? -



wpf - Can I bind the visibility of a button to the IsSelected property of its containing ItemTemplate? -

i working on windows store app. 1 page contains listview has stackpanel textblocks , 3 buttons:

i have 3 buttons visible when listitem belong selected. i've tried every possible arrangement of binding on visibility property of buttons, nil has worked far. appears constrained set of binding types winrt apps preventing me using known solution. there can do?

i'm trying think through perspective of manipulating object in code within listview selectionchanged event handler, nil has worked out yet.

here xaml itemtemplate:

<listview.itemtemplate> <datatemplate> <stackpanel flowdirection="lefttoright" orientation="horizontal"> <stackpanel style="{staticresource sessionblock}" doubletapped="stackpanel_doubletapped"> <textblock style="{staticresource sessiontitle}" text="{binding title}" /> <textblock style="{staticresource sessionstepcount}"> <run text="{binding stepscount}" /> <run text=" steps" /> </textblock> </stackpanel> <button content="&#xe17e;&#xe102;" style="{staticresource actionbutton}" /> <button content="&#xe17e;&#xe104;" style="{staticresource actionbutton}" /> <button content="&#xe17e;&#xe107;" style="{staticresource actionbutton}" /> </stackpanel> </datatemplate> </listview.itemtemplate>

you can leverage templatedparent same

to begin declare bool visibility converter in resources

eg

<booleantovisibilityconverter x:key="booleantovisibilityconverter"/>

then in xaml apply next binding visibility, above declared converter

visibility="{binding templatedparent.isselected,relativesource={relativesource templatedparent},converter={staticresource booleantovisibilityconverter}}"

so trick here find templatedparent of datatemplate content presenter , templatedparent listviewitem , isselected property looking for

so illustration follows

<listview.itemtemplate> <datatemplate> <stackpanel flowdirection="lefttoright" orientation="horizontal"> <stackpanel style="{staticresource sessionblock}" doubletapped="stackpanel_doubletapped"> <textblock style="{staticresource sessiontitle}" text="{binding title}" /> <textblock style="{staticresource sessionstepcount}"> <run text="{binding stepscount}" /> <run text=" steps" /> </textblock> </stackpanel> <button content="&#xe17e;&#xe102;" style="{staticresource actionbutton}" visibility="{binding templatedparent.isselected,relativesource={relativesource templatedparent},converter={staticresource booleantovisibilityconverter}}" /> <button content="&#xe17e;&#xe104;" style="{staticresource actionbutton}" visibility="{binding templatedparent.isselected,relativesource={relativesource templatedparent},converter={staticresource booleantovisibilityconverter}}" /> <button content="&#xe17e;&#xe107;" style="{staticresource actionbutton}" visibility="{binding templatedparent.isselected,relativesource={relativesource templatedparent},converter={staticresource booleantovisibilityconverter}}" /> </stackpanel> </datatemplate> </listview.itemtemplate>

in illustration above applied same to 3 buttons, can set of them in stack panel , apply single binding shorten code.

wpf xaml winrt-xaml

No comments:

Post a Comment