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 内容发生改变,以达到切换视图的效果
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器