c# - WPF how to detect listviewitem has been selected -
i have datatemplate listview control, contains few textblocks , button. want button visible when item selected. here's datatemplate code:
<listview.itemtemplate> <datatemplate> <grid background="aliceblue"> <grid.columndefinitions> <columndefinition width="300" /> <columndefinition width="*" /> <columndefinition width="90" /> </grid.columndefinitions> <textblock horizontalalignment="left" margin="10,0,10,0" verticalalignment="center" fontfamily="verdana" fontsize="16" fontweight="black" grid.column="0" text="{binding name}"/> <stackpanel orientation="horizontal" horizontalalignment="center" grid.column="1"> <textblock fontfamily="verdana" fontsize="10" grid.column="1" verticalalignment="center" horizontalalignment="center" text="number of chapters: " /> <textblock fontfamily="verdana" fontsize="12" grid.column="1" verticalalignment="center" horizontalalignment="center" text="{binding chapterscount}" /> </stackpanel> <button horizontalalignment="center" height="50" width="80" content="read" grid.column="2" visibility="hidden" click="button_click_3" name="readbutton"/> </grid> </datatemplate> </listview.itemtemplate>
how can that?
you can achieve datatrigger
in datatemplate check isselected
property of templated parent:
<listview.itemtemplate> <datatemplate> ..... <button horizontalalignment="center" height="50" width="80" content="read" grid.column="2" visibility="hidden" name="readbutton"/> </grid> <datatemplate.triggers> <datatrigger binding="{binding isselected, relativesource={relativesource mode=templatedparent}}" value="true"> <setter targetname="readbutton" property="visibility" value="visible"/> </datatrigger> </datatemplate.triggers> </datatemplate> </listview.itemtemplate>
Comments
Post a Comment