wpf - Use checkbox as togglebutton in expander -
im quite new wpf , have following problem.
i need create list (i using listbox) of items can expanded (expander). problem is, can expanded, if have been 'selected'. each listboxitem should have checkbox , text.
so basic example illustrate mean:
<listbox> <item>(checkbox) john doe</item> <item>(checkbox) mike murray</item> </listbox>
if (so multiple allowed) of checkboxes in listbox checked, item expands showing more data.
again example:
<listbox> <item> (checkbox-checked) john doe data shown in expanded area </item> <item> (checkbox-unchecked) mike murray</item> </listbox>
i cant expander use checkbox 'togglebutton'.
could me out? example code welcome...
this should trick:
<listbox> <listbox.resources> <style targettype="expander"> <setter property="template"> <setter.value> <controltemplate targettype="expander"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <checkbox ischecked="{binding path=isexpanded, relativesource={relativesource templatedparent}}" content="{templatebinding header}" /> <contentcontrol x:name="body" grid.row="1" content="{templatebinding content}" /> </grid> <controltemplate.triggers> <trigger property="isexpanded" value="false"> <setter targetname="body" property="visibility" value="collapsed" /> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.resources> <expander header="one"> content 1 </expander> <expander header="two"> content 2 </expander> </listbox>
i've defined style
here changes template
of expander
controls style
applied. (and since i've put style
in listbox.resources
it'll automatically apply expander
controls in list.)
the trick getting checkbox
work when put (or indeed togglebutton
based control) expander
template, need use data binding configured relativesource
set templatedparent
. enables two-way binding - means not checkbox
reflect current state of expander, able change current state.
Comments
Post a Comment