GridView HeaderTemplate Lost DataContext
I have a gridview with HeaderTemplate, I put a butt开发者_如何转开发on on the header, and have a command attached to it, my ViewModel never gets called. How do I trace back my ViewModel, seems like it gets "lost".
any ideas ?
using this:
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}
will bring me back to my DataContext
.
Column HeaderTemplate is a generated control part of the DataGrid and does not have direct VisualTree access to the DataGrid's DataContext, so you have to add RelativeSource AncestorType and explicitly refer to the property via the DataContext
"{Binding DataContext.AllSelected, RelativeSource={RelativeSource AncestorType=Window}}" />
or you can also write this to you top element in template
DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}"
精彩评论