How do I bind a WPF control's content to its container's DataContext so I can apply a DataTemplateSelector based on that object? -
i'm trying bind wpf window atop viewmodel contains 2 collections, , b. i'm attempting use datatemplates display either or b depending on setting of flag in viewmodel.
to end, i've set window's datacontext = viewmodel
. however, when attempt bind contentcontrol
datacontext , apply datatemplateselector
it, item
parameter of selector's selecttemplate(object item, dependencyobject container)
method null:
<window [snip] title="mainwindow"> <window.resources> <!-- datatemplate , selector declarations --> </window.resources> <grid> <contentcontrol content="{binding}" contenttemplateselector="{staticresource templateselector}" /> </grid> </window>
how should binding contentcontrol
such window's viewmodel passed through datatemplateselector
?
this worked me:
<contentcontrol content="{binding datacontext, relativesource={relativesource self}}" contenttemplateselector="{staticresource templateselector}" />
edit:
i agree aaron though, might not best way accomplish things. said you're using viewmodel. easiest way bind itemscontrol new "selectedcollection" property on viewmodel exposes wanted collection. in flag (assuming property) can fire propertychanged "selectedcollection".
Comments
Post a Comment