Reputation: 4386
I am trying to get some binding code working. Bascially I want to bind the IsEnabled property of an element of my grid's context menu with a value of the selected row in the grid.
I have it working with this:
<my:DataGrid.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Grant Access" IsEnabled="{Binding Connectable}"/>
</ContextMenu>
</my:DataGrid.ContextMenu>
But I want to do it this way and it's not working. It doesn't error but just doesn't disable the menu item. Any idea why?
<my:DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Grant Access" IsEnabled="{Binding Path=SelectedItem.Connectable, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type my:DataGrid}}}"/>
</ContextMenu>
</my:DataGrid.ContextMenu>
Upvotes: 3
Views: 7075
Reputation: 30840
Try using ElementName binding
instead of Ancestor binding
. ContextMenu
is not part of Grid's visual tree.
--edit--
Ah, I was wrong. ElementName binding (example given below) would also not work with ContextMenu. It is not part of DataGrid's visual tree. That is why it cannot see that DataGrid and thus cannot reference it. You will have to do it using the first method.
Any reason why you don't want to do it that way?
<DataGrid.ContextMenu>
<ContextMenu DataContext="{Binding SelectedItem, ElementName=DataGrid1}">
<MenuItem Header="Grant Access"
IsEnabled="{Binding Connectable}" />
</ContextMenu>
</DataGrid.ContextMenu>
Upvotes: 2
Reputation: 20471
If you look at the output window in Visual Studio while in Debug mode, you'll get details of the binding error - which may shed some light on your problem.
Upvotes: 0