Skip to content

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

@gentledepp

Description

@gentledepp

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 :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions