wpf - Bind DataTemplate's parent's visibility (IsVisible or Visibility) to the ViewModel -
starting off classic datatemplate:
<datatemplate x:key="regulartemplate"> <grid> ... </grid> </datatemplate>
assume viewmodel object being rendered using above template has following property:
private visibility _visibility; public visibility vmvisibility { set { if (value == _visibility) return; _visibility = value; if (value == system.windows.visibility.visible) { viewrefresher.twentysecondstick += tick; } else { viewrefresher.twentysecondstick -= tick; } } private { return _visibility; } }
i want setter code somehow run when listboxitem not rendered panel containing it. it's custom panel hides/shows items during scrolling, need bind listboxitems's visibility somehow.
i've tried stuff along lines of:
<datatemplate x:key="regulartemplate"> <grid> ... </grid> <datatrigger binding="{binding isvisible,relativesource= {relativesource findancestor, ancestortype={x:type listboxitem}}}" value="false"> <setter property="{binding vmvisibility}" value="false"/> </datatrigger> </datatemplate>
but can't use datatriggers way.
any ideas?
don't hate when spend 2 hours searching solution, give up, post on stackoverflow, find 5 minutes later?
answer:
using guy's behaviour: https://stackoverflow.com/a/3667609/855551
you can bind isvisible of parent viewmodel:
<datatemplate x:key="regulartemplate"> <grid> <behaviour:datapiping.datapipes> <behaviour:datapipecollection> <behaviour:datapipe source="{binding relativesource= {relativesource ancestortype={x:type listboxitem}}, path=isvisible}" target="{binding path=visible, mode=onewaytosource}"/> </behaviour:datapipecollection> </behaviour:datapiping.datapipes> ... </grid> </datatemplate>
vm object:
private bool _visible; public bool visible { set { if (value == _visible) return; _visible = value; if (value == true) enablerefresh(); else disablerefresh(); } private { return _visible; } }
Comments
Post a Comment