Morse
Morse

Reputation: 9134

Customize NavigationView panel to include image UWP

I am trying to customize NavigationView on UWP C# XAML to have static app image at the top side of left panel which wont redirect to a page but rather just the app icon.

The official documentation only mentions displaying App title using Canvas.Index 0/1 option and customizing background.

<NavigationView OpenPaneLength="200"
                x:Name="navigationView"
                SelectedItem="{x:Bind ViewModel.Selected, Mode=OneWay}"
                Header="{x:Bind ViewModel.Selected.Content, Mode=OneWay}"
                IsSettingsVisible="True"
                IsPaneToggleButtonVisible="True"
                Background="{ThemeResource SystemControlBackgroundAltHighBrush}">

Corresponding Nav panel

I would like to customize this to have an at least 100x100 pixels image, at the top.

What have I tried? : I tried the below code, but the image occupies only the icon space at the left.

<NavigationViewItem>
    <Image Source="/Assets/Cubes/purple.png"
           HorizontalAlignment="Center"></Image>
</NavigationViewItem>

Image 2

<NavigationViewItem>
    <Image Source="/Assets/Cubes/purple.png"
           HorizontalAlignment="Center" MinHeight="100" MinWidth="100"></Image>
</NavigationViewItem>

image 3

<NavigationViewItem Height="100">
    <Image Source="/Assets/Cubes/purple.png"
           HorizontalAlignment="Center" MinHeight="100" MinWidth="100"></Image>
</NavigationViewItem>

image 4

Expected

enter image description here


Is it still possible to achieve this with NavigationView or do I need to use other UI elements like SplitView or MasterDetailView

Upvotes: 3

Views: 4318

Answers (2)

Mubarrat Hasan
Mubarrat Hasan

Reputation: 304

It is worked for me. Example:
Write <NavigationViewItemHeader> in <NavigationView.MenuItems>. like

<NavigationView.MenuItems>
    <NavigationViewItemHeader Height="180">
        <Image Source="/Assets/Cubes/purple.png" Width="320" Height="180"/>
    </NavigationViewItemHeader>
</NavigationView.MenuItems>

Upvotes: 0

Xie Steven
Xie Steven

Reputation: 8601

The issue is due to the root panel's height of the NavigationViewItem's ControlTemplate is set to a fixed value(40).

You could edit the NavigationViewItem's style to make it meet your requirement.

Please see my following sample:

<Style TargetType="NavigationViewItem" x:Key="test">
        <Setter Property="Foreground" Value="{ThemeResource NavigationViewItemForeground}" />
        <Setter Property="Background" Value="{ThemeResource NavigationViewItemBackground}" />
        <Setter Property="BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrush}" />
        <Setter Property="BorderThickness" Value="{StaticResource NavigationViewItemBorderThickness}" />
        <Setter Property="UseSystemFocusVisuals" Value="True" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="NavigationViewItem">
                    <Grid
                    x:Name="LayoutRoot"
                    Height="{TemplateBinding Height}"
                    Background="{TemplateBinding Background}"
                    Control.IsTemplateFocusTarget="True">

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="PointerStates">
                                <VisualState x:Name="Normal" />

                                <VisualState x:Name="PointerOver">
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.(RevealBrush.State)" Value="PointerOver" />
                                        <Setter Target="LayoutRoot.Background" Value="{ThemeResource NavigationViewItemBackgroundPointerOver}" />
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushPointerOver}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource NavigationViewItemForegroundPointerOver}" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="Pressed">
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.(RevealBrush.State)" Value="Pressed" />
                                        <Setter Target="LayoutRoot.Background" Value="{ThemeResource NavigationViewItemBackgroundPressed}" />
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushPressed}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource NavigationViewItemForegroundPressed}" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.Background" Value="{ThemeResource NavigationViewItemBackgroundSelected}" />
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushSelected}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource NavigationViewItemForegroundSelected}" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="PointerOverSelected">
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.(RevealBrush.State)" Value="PointerOver" />
                                        <Setter Target="LayoutRoot.Background" Value="{ThemeResource NavigationViewItemBackgroundSelectedPointerOver}" />
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushSelectedPointerOver}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource NavigationViewItemForegroundSelectedPointerOver}" />
                                    </VisualState.Setters>
                                </VisualState>

                                <VisualState x:Name="PressedSelected">
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.(RevealBrush.State)" Value="Pressed" />
                                        <Setter Target="LayoutRoot.Background" Value="{ThemeResource NavigationViewItemBackgroundSelectedPressed}" />
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushSelectedPressed}" />
                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource NavigationViewItemForegroundSelectedPressed}" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="DisabledStates">
                                <VisualState x:Name="Enabled" />

                                <VisualState x:Name="Disabled">
                                    <VisualState.Setters>
                                        <Setter Target="RevealBorder.BorderBrush" Value="{ThemeResource NavigationViewItemBorderBrushCheckedDisabled}" />
                                        <Setter Target="LayoutRoot.Opacity" Value="{ThemeResource ListViewItemDisabledThemeOpacity}" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="IconStates">
                                <VisualState x:Name="IconVisible" />
                                <VisualState x:Name="IconCollapsed">
                                    <VisualState.Setters>
                                        <Setter Target="IconBox.Visibility" Value="Collapsed" />
                                        <Setter Target="IconColumn.Width" Value="16" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>

                        <!-- Wrap SelectionIndicator in a grid so that its offset is 0,0 - this enables the offset animation. -->
                        <Grid 
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center">

                            <Rectangle
                            x:Name="SelectionIndicator"
                            Width="6"
                            Height="24"
                            Fill="{ThemeResource NavigationViewSelectionIndicatorForeground}"
                            Opacity="0.0"/>
                        </Grid>

                        <Border
                        x:Name="RevealBorder"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" />

                        <Grid Height="100" HorizontalAlignment="Left" x:Name="ContentGrid">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="IconColumn" Width="48" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Viewbox x:Name="IconBox"
                            Child="{TemplateBinding Icon}"
                            Margin="16,12"/>

                            <ContentPresenter x:Name="ContentPresenter"
                            Grid.Column="1"
                            ContentTransitions="{TemplateBinding ContentTransitions}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Content="{TemplateBinding Content}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            Margin="{TemplateBinding Padding}"/>

                            <ToolTipService.ToolTip>
                                <ToolTip x:Name="ToolTip" />
                            </ToolTipService.ToolTip>
                        </Grid>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I set Height="{TemplateBinding Height}" for the 'LayoutRoot' Grid, then you could apply this style to your NavigationViewItem like the following:

<NavigationViewItem Style="{StaticResource test}">
    <Image Source="/Assets/Cubes/purple.png" HorizontalAlignment="Center" MinHeight="100" MinWidth="100"></Image>
</NavigationViewItem>

Upvotes: 2

Related Questions