【问题标题】:Handling WPF TabItems Visibility property处理 WPF TabItems Visibility 属性
【发布时间】:2013-02-18 18:45:00
【问题描述】:

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

我也尝试了here 中提到的以下方法,但没有运气。

有什么方法可以让TabItems 中的内容隐藏并选择可见的选项卡。

【问题讨论】:

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

标签: c# wpf visibility tabitem


【解决方案1】:

你不需要这些。从概念上讲,TabControl 只是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(在每个选项卡内部创建适当的逻辑),并为app.xaml 中的每个派生类中的每一个适当的DataTemplate 或其他东西.

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

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

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

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

【讨论】:

  • 谢谢。我将对此进行处理并进行更新。该代码似乎是我正在寻找的。​​span>
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多