gpt4 book ai didi

c# - 处理 WPF TabItems 可见性属性

转载 作者:太空狗 更新时间:2023-10-29 22:33:03 27 4
gpt4 key购买 nike

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

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

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

最佳答案

你不需要这些。从概念上讲,一个 TabControl只是 ObservableCollection<ViewModel> 的图形表示,其中每个 View 模型都由一个选项卡项表示,并且只有 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},
}
};
}
}

View 模型:

    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 中的每一个派生类什么的。

每当您想从 View 中删除选项卡项时,您无需操作 View ,而是操作 ViewModel。这是适用于所有内容的 WPF 方法。它消除了在代码中操作复杂对象(UI 元素)的需要,从而简化了一切。每当你设置

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

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

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

关于c# - 处理 WPF TabItems 可见性属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15205626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com