7

我一直在阅读Visibility.Collapsed关于TabItems. 当Visibility设置为Collapsed时,TabItem标题被隐藏,但内容仍然可见。

我也尝试过这里提到的以下方法,但没有运气。

有没有办法让里面的内容TabItems隐藏起来,并选择可见的选项卡。

4

2 回答 2

11

你不需要这些。从概念上讲, aTabControl只是 a 的图形表示,其中每个视图模型由一个选项卡项表示,并且在给定时间ObservableCollection<ViewModel>只有 1个:SelectedItem

<Window x:Class="WpfApplication4.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window12" Height="300" Width="300">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    </Window.Resources>
        <TabControl ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
                    <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}"/>
                    <Setter Property="Header" Value="{Binding Title}"/>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
</Window>

代码背后:

using System.Windows;
using BaseFramework.MVVM;
using System.Collections.ObjectModel;

namespace WpfApplication4
{
    public partial class Window12 : Window
    {
        public Window12()
        {
            InitializeComponent();
            DataContext = new TabbedViewModel()
                          {
                              Items =
                                  {
                                      new TabViewModel() {Title = "Tab #1", IsEnabled = true, IsVisible = true},
                                      new TabViewModel() {Title = "Tab #2", IsEnabled = false, IsVisible = true},
                                      new TabViewModel() {Title = "Tab #3", IsEnabled = true, IsVisible = false},
                                  }
                          };
        }
    }

视图模型:

    public class TabbedViewModel: ViewModelBase
    {
        private ObservableCollection<TabViewModel> _items;
        public ObservableCollection<TabViewModel> Items
        {
            get { return _items ?? (_items = new ObservableCollection<TabViewModel>()); }
        }

        private ViewModelBase _selectedItem;
        public ViewModelBase SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyPropertyChange(() => SelectedItem);
            }
        }
    }

    public class TabViewModel: ViewModelBase
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChange(() => Title);
            }
        }

        private bool _isEnabled;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                NotifyPropertyChange(() => IsEnabled);
            }
        }

        private bool _isVisible;
        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                _isVisible = value;
                NotifyPropertyChange(() => IsVisible);
            }
        }
    }
}

然后,它只是继承TabViewModel每个选项卡的问题(在每个选项卡内部创建适当的逻辑),并DataTemplate为这些派生类中的每一个app.xaml或其他东西提供适当的。

每当您想从视图中删除选项卡项时,您都可以操作 ViewModel,而不是操作视图。这是所有事情的 WPF 方法。它消除了在代码中操作复杂对象(UI 元素)的需要,从而简化了一切。每当你设置

TabbedViewModel.SelectedItem.IsVisible = false;,请确保您还这样做:

TabbedViewModel.SelectedItem = TabbedViewModel.Items.First(x => x.IsVisible && x.IsEnabled);

这将防止您陷入将不可见的选项卡项作为选定项的情况。

于 2013-03-04T16:25:46.170 回答
1

您好,只需从 TabControl 添加和删除 TabItems,而不是设置可见性。打开和关闭可见性还有一个问题,当您滚动或最小化或调整 TabControl 大小时,它会出现异常,例如超出索引。

于 2013-03-04T16:07:43.710 回答