HamburgerMenu doesn't handle DataContext inheritance for Menu items
See original GitHub issueI’m trying tu use HamburgerMenu with custom UserControls as items. Unfortunately, the Datacontext link is not propagated to the items usercontrol.
<Border Grid.Column="0"
Margin="10 10 0 10"
BorderBrush="{DynamicResource GrayBrush7}"
BorderThickness="1">
<Controls:HamburgerMenu x:Name="HamburgerMenuControl"
HamburgerWidth="40"
IsPaneOpen="True"
ItemInvoked="HamburgerMenuControl_OnItemInvoked"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"
SelectedIndex="2"
Style="{StaticResource HamburgerMenuCreatorsStyle}"
VerticalScrollBarOnLeftSide="False">
<!-- Items -->
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuIconItem Icon="{iconPacks:Modern Kind=Clean}" Label="Clean">
<Controls:HamburgerMenuIconItem.Tag>
<views:Clean/>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
I added in code behind the following event
private void HamburgerMenuControl_OnItemInvoked(object sender, HamburgerMenuItemInvokedEventArgs e)
{
HamburgerMenuControl.Content = e.InvokedItem;
// Set the DataContext to the UserControl created dynamically when clicking on the menu
((e.InvokedItem as HamburgerMenuIconItem).Tag as UserControl).DataContext = this.DataContext;
}
This solve the problem, but not for the first time. Indeed when the application starts, the event is not invoked. Adding this kind of XAML code doesn’t work also.
DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
Any idea ? Thank you
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Set the data context of a custom control of mahapps ...
After this, I created a dependency property in my hamburger menu user control so that I can pass the view model from the...
Read more >ContextMenu in HamburgerMenu binding to ViewModel
i have a HamburgerMenu in my Window but binding the command of a MenuItem in a ContextMenu on a HamburgerMenuNavigationButton does not work....
Read more >HamburgerMenu Properties | WPF Controls
Hide Inherited. Represents a collapsible navigation menu that displays a stack of navigation buttons. Supports regular buttons, sub-menus, hyperlinks, check and ...
Read more >MahApps.Metro v2.0.0 release notes (2020-05-24) | LibHunt
#3374 [DateTimePicker] - Focus and BorderBrush; #3367 HamburgerMenu doesn't handle DataContext inheritance for Menu items; #3365 Wrong foreground/text color ...
Read more >[Solved]-Binding value not passed to user control in WPF-wpf
The window can get away with setting its own DataContext to Self because it has no parent, so it's not stepping on an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There are 3 options to solve this “known” problem:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SomeProperty; DataItem=null; target element is ‘SomeElement’ (HashCode=123456789); target property is ‘AProperty’ (type ‘TypeOfTheProperty’)
This could be solved…
I have just come across precisely the same issue, same symptoms as OP. The bizarre thing is that if I use Snoop and hit the control suddenly the DataContext gets set correctly. I’ve tried the binding proxy trick, it works but like the OP said not the first time, unless I use Snoop to “prod” it into action.