I want to reuse a control, but one of the scenarios requires a context menu and the others do not. Here's my attempt.
public partial class RP8Grid : UserControl {
public bool UseContextMenu {
get { return (bool)GetValue(UseContextMenuProperty); }
set { SetValue(UseContextMenuProperty, value); }
}
// Using a DependencyProperty as the backing store for UseContextMenu. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UseContextMenuProperty =
DependencyProperty.Register("UseContextMenu", typeof(bool), typeof(RP8Grid), new PropertyMetadata(false));
public RP8Grid() {
InitializeComponent();
}
}
And in the XAML to use the Property:
<ctls:RP8Grid UseContextMenu="False"/>
Now the part I cannot square away, how do I access UseContextMenu inside the UserControl? I Have tried the following:
<DataGrid>
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="{Binding UseContextMenu,RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}}">
</DataGrid.ContextMenu>
</DataGrid>
with results:
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1'
<ContextMenu IsEnabled={Binding UseContextMenu, RelativeSource={RelativeSource AncestorType=UserControl}}" />
– 15ee8f99-57ff-4f92-890c-b56153