Skip to content

Using a control as NavigationBar.Header or .HeaderIcon causes runtime exception #76

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

Open
gentledepp opened this issue Mar 21, 2025 · 0 comments · May be fixed by #77
Open

Using a control as NavigationBar.Header or .HeaderIcon causes runtime exception #76

gentledepp opened this issue Mar 21, 2025 · 0 comments · May be fixed by #77

Comments

@gentledepp
Copy link
Contributor

When using controls as header and heradericon like so:

  <NavigationBar.HeaderIcon>
        <PathIcon Data="{StaticResource list_regular}"
                  Foreground="{DynamicResource IconColor}" />
    </NavigationBar.HeaderIcon>
    <NavigationBar.Header>
        <Button Content="Header"
                Background="#00000000"></Button>
    </NavigationBar.Header>

and having a TabPage (which also shows them in its tab)

you get a runtime exception:

System.InvalidOperationException: The control Button (Content = Header) already has a visual parent ContentPresenter (Name = PART_ContentPresenter, Host = ContentControl) while trying to add it as a child of ContentPresenter (Name = PART_ContentPresenter, Host = TransitioningContentControl (Name = PART_Header)).

Image

To mitigate this, I propose to introduce two additional properties:

  • NavigationBar.HeaderIconTemplate
  • NavigationBar.HeaderTempalte

You can then use these to setup your view like:

            .... 
             NavigationBar.Header="My Activities"
             NavigationBar.HeaderIcon="{StaticResource activity_regular}">

    <NavigationBar.HeaderIconTemplate>
        <DataTemplate x:DataType="StreamGeometry">
            <PathIcon Data="{Binding .}"  />
        </DataTemplate>
    </NavigationBar.HeaderIconTemplate>
    <NavigationBar.HeaderTemplate>
        <DataTemplate x:DataType="{x:Type system:String}">
            <TextBlock Text="{Binding .}" />
        </DataTemplate>
    </NavigationBar.HeaderTemplate>

and you're good to go.
As far as I understand, this should also be backward compatible.

PR is following! I hope you like it @OmidID :-)

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

Successfully merging a pull request may close this issue.

1 participant