自定义NavigationView面板以包含图像UWP

时间:2018-07-20 16:21:10

标签: c# xaml uwp uwp-xaml

我正在尝试自定义UWP C#XAML上的NavigationView,以在左侧面板的顶部具有静态应用程序图像,该图像不会重定向到页面,而只是重定向到应用程序图标。

官方documentation仅提及使用Canvas.Index 0/1选项显示应用程序标题并自定义背景。

<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

我想自定义此图片,使其顶部至少有100x100像素的图像。

我尝试了什么? : 我尝试了以下代码,但是图像仅占据左侧的图标空间。

<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

期望

enter image description here


是否仍然可以通过NavigationView来实现这一目标,或者我是否需要使用其他SplitViewMasterDetailView等UI元素

2 个答案:

答案 0 :(得分:1)

此问题是由于NavigationViewItem的ControlTemplate的根面板的高度设置为固定值(40)。

您可以编辑NavigationViewItem的样式以使其符合您的要求。

请参阅我的以下示例:

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

我为“ LayoutRoot”网格设置了Height="{TemplateBinding Height}",然后您可以将这种样式应用于NavigationViewItem,如下所示:

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

答案 1 :(得分:0)

它对我有用。示例:
<NavigationViewItemHeader> 中写入 <NavigationView.MenuItems>。喜欢

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