0

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'

6
  • 1
    You are right, I think I hit propa instead of propdp. My first implementation. RelativeSource, then up thru the ancestor chain? Commented Jul 13, 2017 at 17:50
  • <ContextMenu IsEnabled={Binding UseContextMenu, RelativeSource={RelativeSource AncestorType=UserControl}}" /> Commented Jul 13, 2017 at 17:52
  • 1
    Couldn't find, have updated the question with my attempt Commented Jul 13, 2017 at 18:28
  • Right, ContextMenus are out of the visual tree; my bad. I was able to do this with a binding proxy (that answer illustrates doing something else with one). However, disabling a contextmenu is problematic: It still opens, but with all its items disabled -- and it doesn't close properly. It might be better to give your DataGrid a Style which assigns it the context menu when that property is true. Commented Jul 13, 2017 at 19:22
  • Ah, good idea. I'll check out this proxy, thank you. Commented Jul 13, 2017 at 19:43

1 Answer 1

3

If what you want is just to get rid of the ContextMenu at times, this will work:

<DataGrid > <DataGrid.Style> <Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}"> <Style.Triggers> <DataTrigger Binding="{Binding UseContextMenu, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="True" > <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu > <MenuItem Header="Test Item" /> <MenuItem Header="Test Item" /> <MenuItem Header="Test Item" /> <MenuItem Header="Test Item" /> </ContextMenu> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </DataGrid.Style> </DataGrid> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.