February 2007 Blog Posts

Binding ContextMenu to its logical Parent

Originally published on my old Charteris blog Richard contacted me today because he was having trouble binding a ListBox’s ContextMenu to data belonging to the SelectedItem. My initial thought was binding statement like this <ListBox.ContextMenu>   <ContextMenu>     <MenuItem Header="{Binding Path=SelectedIndex,                     RelativeSource={RelativeSource FindAncestor,                     AncestorType={x:Type ListBox}}}" />   </ContextMenu> </ListBox.ContextMenu> RelativeSource tells the binding looking for the source of the binding and the way it does this is dependent on the mode. The four, supported modes are FindAncestor...