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 @ 2022-07-20 17:45  害羞的青蛙  阅读(1250)  评论(1)    收藏  举报