WPF 导航页面面包屑

发布于 2024-08-27 03:26:47 字数 3506 浏览 6 评论 0原文

我找到了使用面包屑而不是页面导航按钮的代码。这段代码非常适合将页面设置为启动。我的问题是我需要一个带有框架控件的窗口作为启动,这导致面包屑根本不显示。我的造型似乎缺少一些东西。类型曾经是 NavigationWindow 但我将它们更改为 Frame 以尝试获得可行的解决方案。

<Style TargetType="Frame" x:Key="{x:Type Frame}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Frame">
                <Grid Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource AncestorType={x:Type Frame}}, 
                        Path=BackStack}">

                        <!--Force the ItemsContol to use a wrap panel as Items host-->
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <local:InverseWrapPanel KeyboardNavigation.TabNavigation="Cycle"
                                            KeyboardNavigation.DirectionalNavigation="Cycle"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>

                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Command="NavigationCommands.NavigateJournal"
                                        CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                                        Content="{Binding Name}">
                                    <Button.Template>
                                        <ControlTemplate TargetType="Button">
                                            <WrapPanel>
                                                <TextBlock Name="text1" FontWeight="Bold" Text="{TemplateBinding Content}"/>
                                                <TextBlock Name="text2" FontWeight="Bold" Text=">>" Margin="2,0,0,0"/>
                                            </WrapPanel>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter TargetName="text1" Property="Foreground" Value="Blue"/>
                                                    <Setter TargetName="text2" Property="Foreground" Value="Blue"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>

                    </ItemsControl>

                    <AdornerDecorator Grid.Row="2">
                        <ContentPresenter Name="PART_NavWinCP" ClipToBounds="true"/>
                    </AdornerDecorator>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I found code to use a breadcrumb instead of the navigation buttons for my pages. This code works perfect with setting a page as the startup. My problem is that I need to have a window with a frame control as the startup and this is causing the breadcrumb to not show at all. I seem to be missing something with my styling. The types used to be NavigationWindow but I changed them to Frame to try and get a working solution.

<Style TargetType="Frame" x:Key="{x:Type Frame}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Frame">
                <Grid Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource AncestorType={x:Type Frame}}, 
                        Path=BackStack}">

                        <!--Force the ItemsContol to use a wrap panel as Items host-->
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <local:InverseWrapPanel KeyboardNavigation.TabNavigation="Cycle"
                                            KeyboardNavigation.DirectionalNavigation="Cycle"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>

                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Command="NavigationCommands.NavigateJournal"
                                        CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                                        Content="{Binding Name}">
                                    <Button.Template>
                                        <ControlTemplate TargetType="Button">
                                            <WrapPanel>
                                                <TextBlock Name="text1" FontWeight="Bold" Text="{TemplateBinding Content}"/>
                                                <TextBlock Name="text2" FontWeight="Bold" Text=">>" Margin="2,0,0,0"/>
                                            </WrapPanel>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter TargetName="text1" Property="Foreground" Value="Blue"/>
                                                    <Setter TargetName="text2" Property="Foreground" Value="Blue"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>

                    </ItemsControl>

                    <AdornerDecorator Grid.Row="2">
                        <ContentPresenter Name="PART_NavWinCP" ClipToBounds="true"/>
                    </AdornerDecorator>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-09-03 03:26:47

将:更改

<ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource AncestorType={x:Type Frame}}, 
                        Path=BackStack}">

为:

<ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource TemplatedParent}, 
                        Path=BackStack}">

并且它应该有效。

Change:

<ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource AncestorType={x:Type Frame}}, 
                        Path=BackStack}">

to:

<ItemsControl ItemsSource="{Binding 
                        RelativeSource={RelativeSource TemplatedParent}, 
                        Path=BackStack}">

and its should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文