WPF利用Prism制作选项卡页面,并且删除指定选项卡

需求背景:

正常我们的界面展示都是单页面显示我们需要的界面,但是当我们需要在当前界面同时显示多个界面,那需要用到选项卡,每个选项卡都有独立的界面提供用户显示,此时我们甚至不需要使用当前的选项卡时我们或选择关闭当前的选项卡

实现步骤:

1:视图正常的注册导航控件,不过现在的导航控件是TabControl

TabControl prism:RegionManager.RegionName="TabRegion"

2:设置当前的TabControl数据模板

复制代码
<StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem}, Path=Content.DataContext.Title}" />
                        <Button
                            Width="20"
                            Height="20"
                            Margin="5,0"
                            Command="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=DataContext.DeleteCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TabItem}, Path=Content}" />
                    </StackPanel>
复制代码

3:在我们的ViewModel中实现DeleteCommand删除命令

复制代码
private DelegateCommand<object> _DeleteCommand;
        public DelegateCommand<object> DeleteCommand =>
            _DeleteCommand ?? (_DeleteCommand = new DelegateCommand<object>(ExecuteDeleteCommand));

        void ExecuteDeleteCommand(object obj)
        {
            if (obj == null) return;
            Region.Regions["TabRegion"].Remove(obj);
        }
复制代码

4:此时运行当前程序,点击按钮即可完成删除选项卡功能以及切换选项卡功能,如图

 

posted @   害羞的青蛙  阅读(1148)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示