aio*_*cle 5 c# wpf visibility tabitem
我一直在阅读有关Visibility.Collapsed的TabItems.当Visibility设置为时Collapsed,TabItem标题被隐藏但内容仍然可见.
我也试过这里提到的以下approch ,但没有运气.
有没有办法获取TabItems要隐藏的内容,还可以选择可见的选项卡.
你不需要任何这些.从概念上讲,a TabControl只是一个图形表示ObservableCollection<ViewModel>,其中每个视图模型由一个标签项表示,并且SelectedItem在给定时间只有1 :
<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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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},
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,它只是继承TabViewModel每个选项卡(在每个选项卡内部创建适当的逻辑),并适用DataTemplate于这些app.xaml或类似的每个派生类.
每当您想要从视图中删除选项卡项时,您可以操纵ViewModel而不是操纵视图.这是针对一切的WPF方法.它通过消除在代码中操作复杂对象(UI元素)的需要简化了一切.每当你设置
TabbedViewModel.SelectedItem.IsVisible = false;,请确保您也这样做:
TabbedViewModel.SelectedItem = TabbedViewModel.Items.First(x => x.IsVisible && x.IsEnabled);
这将防止您遇到将不可见标签项作为所选项目的情况.
您好,只需从 TabControl 添加和删除 TabItem,而不是设置可见性。打开和关闭可见性还有一个问题,当您滚动、最小化或调整 TabControl 大小时,会出现类似 Out of index 的异常情况。
| 归档时间: |
|
| 查看次数: |
10505 次 |
| 最近记录: |