Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HamburgerMenu doesn't handle DataContext inheritance for Menu items #3367

Closed
damusthe opened this issue Nov 11, 2018 · 6 comments
Closed

HamburgerMenu doesn't handle DataContext inheritance for Menu items #3367

damusthe opened this issue Nov 11, 2018 · 6 comments
Labels
Milestone

Comments

@damusthe
Copy link

damusthe commented Nov 11, 2018

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

@punker76
Copy link
Member

punker76 commented Jan 2, 2019

@damusthe Can you create a short sample which shows your issue? Thx!

@punker76
Copy link
Member

punker76 commented Jan 8, 2019

@damusthe ping

@Laurnz
Copy link

Laurnz commented Jan 9, 2019

I have the exact same Problem. I need some views in the HamburgerMenu to bind to the same ViewModel als to control the code is in, but setting the DataContext is doing nothing in the XAML code.

Currently the code looks something like this:

<controls:HamburgerMenu x:Name="createProject_hamburgerMenuMain"
        ItemTemplate="{DynamicResource Global_Template_HamburgerMenuItem}"
        OptionsItemTemplate="{DynamicResource Global_Template_HamburgerMenuItem}"
        Style="{DynamicResource Global_Style_HamburgerMenuCreators}"
        SelectedIndex="0">

    <!--  Items  -->
    <controls:HamburgerMenu.ItemsSource>
        <controls:HamburgerMenuItemCollection>
            <controls:HamburgerMenuGlyphItem Glyph="&#xE946;" Label="Project info">
                <controls:HamburgerMenuGlyphItem.Tag>
                    <views:View_CreateProject_General DataContext="{Binding}"/>
                </controls:HamburgerMenuGlyphItem.Tag>
            </controls:HamburgerMenuGlyphItem>

            <controls:HamburgerMenuGlyphItem Glyph="&#xE71D;" Label="Jobs">
                <controls:HamburgerMenuGlyphItem.Tag>
                    <views:View_CreateProject_Jobs DataContext="{Binding}" />
                </controls:HamburgerMenuGlyphItem.Tag>
            </controls:HamburgerMenuGlyphItem>
        </controls:HamburgerMenuItemCollection>
    </controls:HamburgerMenu.ItemsSource>

    <!--  Content  -->
    <controls:HamburgerMenu.ContentTemplate>
        <DataTemplate DataType="{x:Type controls:HamburgerMenuItem}">
            <ContentControl x:Name="TheContent"
                Grid.Row="1"
                Focusable="False"
                Foreground="{DynamicResource BlackBrush}"
                Content="{Binding Tag}" />

        </DataTemplate>
    </controls:HamburgerMenu.ContentTemplate>
</controls:HamburgerMenu >

If I just put my view in the code without the HamburgerMenu everything is binding fine:

<views:View_CreateProject_General DataContext="{Binding}" />

@Laurnz
Copy link

Laurnz commented Jan 9, 2019

I found the "problem" in this StackOverflow thread: https://stackoverflow.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor

According to the awnser is the HamburgerMenuGlyphItem is not be part of the visual or logical tree, there is no binding ancestor for the DataContext. i.e. the binding has no source to bind to. So to work around this you have to add a binding proxy.

Is this something that is fixable or just the way the control works?

@SkinnySackboy
Copy link

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.

@punker76 punker76 added this to the 2.0.0 milestone Apr 17, 2019
punker76 added a commit that referenced this issue Apr 29, 2019
…heritance for Menu items

If we want to bind to e.g. a DataContext from the HamburgerMenuItem it could be that we get this error:

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 in 3 ways:

- by using a BindingProxy
- by using x:Reference
- or simply adding the x:Name to the element
punker76 added a commit that referenced this issue Apr 29, 2019
The TransitioningContentControl sets the Visibility at this visual state to collapsed which is wrong. It should only set the opacity to 0.
@punker76
Copy link
Member

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants