This is a migrated thread and some comments may be shown as answers.

Unable to bind itemsource to RadContextMenu

1 Answer 203 Views
Menu
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 29 May 2020, 11:06 AM

Hi, 

I would like to bind my itemsource to my view model using FindAncestor but it fails to find my usercontrol.
ItemSource ="{Binding DataContext.ContextMenuItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}"

However, by binding like this, without specifying the data context:

ItemSource ="{Binding ContextMenuItems}"

It binds to my view model properly for the first time and does not bind properly when i reopen the context menu and gives me the object type as its header... 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 03 Jun 2020, 10:49 AM

Hello Jasmine,

The relative source binding may not work in some situations with RadContextMenu. This is because the menu opens in a Popup which is not part of the visual tree where the UserControl is defined. On the other hand the RelativeSource searches the visual tree of the current element, but because this is different than the one where the UserControl is defined, the binding cannot find anything.

To achieve your requirement you can bind to the UIElement property of RadContextMenu and use its DataContext if suitable for your case. The UIElement property points to the host of the context menu.

I also tested the binding from your second snippet and it works properly on my side.

I've attached a small sample showing both data bindings. Can you please give it a try and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Menu
Asked by
JP
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or