question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HamburgerMenu doesn't handle DataContext inheritance for Menu items

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
punker76commented, Apr 29, 2019

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…

<Controls:HamburgerMenuIconItem x:Name="AboutOption"
                                Command="{Binding Source={x:Reference theHamburgerMenuElement}, Path=DataContext.ShowHamburgerAboutCommand}"
                                Label="About">
    <Controls:HamburgerMenuIconItem.Icon>
        <iconPacks:PackIconMaterial Width="22"
                                    Height="22"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Kind="Help" />
    </Controls:HamburgerMenuIconItem.Icon>
</Controls:HamburgerMenuIconItem>
  • or simply adding the x:Name to the element SO
<Controls:HamburgerMenuIconItem x:Name="AboutOption"
                                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Controls:HamburgerMenu}, Path=DataContext.ShowHamburgerAboutCommand}"
                                Label="About">
    <Controls:HamburgerMenuIconItem.Icon>
        <iconPacks:PackIconMaterial Width="22"
                                    Height="22"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Kind="Help" />
    </Controls:HamburgerMenuIconItem.Icon>
</Controls:HamburgerMenuIconItem>
0reactions
SkinnySackboycommented, Mar 31, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found