WPF开发中遇到的新知识 -- 4

使用ListBox作为导航栏,实现视图跳转

在顶部导航栏的布局设计中,需要一个容器装着一系列视图的标签,如果这个标签是用Button来实现的,需要更改Button的控件模板,会稍微有点麻烦,然后我是使用ListBox,内部就是简单的TextBlock,然后在ListBox的内部添加交互行为,因为TextBlock是没有类似Button的点击效果的,所以需要借助微软的behaviors来实现

<ListBox ItemsSource="{Binding MenuList}" BorderThickness="0">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="SelectionChanged">
            <behavior:InvokeCommandAction
                Command="{Binding NavigateCommand}"
                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBox},Path=SelectedItem}" />
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" Margin="10 0" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<ContentControl Grid.Row="1" prism:RegionManager.RegionName="{x:Static common:RegionNameHelper.MainWindowRegionName}" />

这样我们在选择不同的ListBox子项的时候,就会触发命令,然后执行视图的跳转,也就是 ContentControl 的内容绑定到我们自定义的属性中,然后我们修改属性,然后引起 ContentControl 内容发生改变,以达到切换视图的效果

posted @   huang1993  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示