C# 处理WPF选项卡项可见性属性
我一直在阅读有关C# 处理WPF选项卡项可见性属性,c#,wpf,visibility,tabitem,C#,Wpf,Visibility,Tabitem,我一直在阅读有关可见性的信息。对于选项卡项,已折叠。当可见性设置为折叠时,选项卡项标题被隐藏,但内容仍然可见 我也尝试了中提到的以下方法,但没有成功 有没有办法让TabItems中的内容隐藏并选择可见的选项卡。Hi只需从TabControl中添加和删除TabItems,而不是设置可见性。打开和关闭可见性还有一个问题,当您滚动、最小化或调整TabControl时,它会像超出索引一样异常。您不需要这些。从概念上讲,TabControl只是一个observedcollection的图形表示,其中每个视
可见性的信息。对于选项卡项
,已折叠
。当可见性
设置为折叠
时,选项卡项
标题被隐藏,但内容仍然可见
我也尝试了中提到的以下方法,但没有成功
有没有办法让
TabItems
中的内容隐藏并选择可见的选项卡。Hi只需从TabControl中添加和删除TabItems,而不是设置可见性。打开和关闭可见性还有一个问题,当您滚动、最小化或调整TabControl时,它会像超出索引一样异常。您不需要这些。从概念上讲,TabControl
只是一个observedcollection
的图形表示,其中每个视图模型由一个选项卡项表示,在给定时间只有一个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>
视图模型:
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);
}
}
}
}
公共类选项卡ViewModel:ViewModelBase
{
私人可观测收集项目;
公共可观测收集项目
{
获取{return\u items???(\u items=newobserveCollection());}
}
私有视图模型库_selectedItem;
公共视图模型库SelectedItem
{
获取{return\u selectedItem;}
设置
{
_选择editem=值;
NotifyPropertyChange(()=>SelectedItem);
}
}
}
公共类选项卡ViewModel:ViewModelBase
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置
{
_标题=价值;
NotifyPropertyChange(()=>标题);
}
}
私人住宅已启用;
公共场所被禁止
{
获取{return}
设置
{
_isEnabled=值;
NotifyPropertyChange(()=>已启用);
}
}
私人住宅可见;
公共图书馆是可见的
{
获取{return\u isVisible;}
设置
{
_isVisible=值;
NotifyPropertyChange(()=>IsVisible);
}
}
}
}
然后,只需为每个选项卡继承TabViewModel
(在每个选项卡中创建适当的逻辑),并为app.xaml
中的每个派生类继承一个适当的数据模板
无论何时要从视图中删除选项卡项,而不是操纵视图,都需要操纵ViewModel。这是WPF的所有方法。它消除了在代码中操作复杂对象(UI元素)的需要,从而简化了一切。你什么时候出发
TabbedViewModel.SelectedItem.IsVisible=false代码>,请确保您还执行以下操作:
TabbedViewModel.SelectedItem=TabbedViewModel.Items.First(x=>x.IsVisible&&x.isnabled)代码>
这将防止您陷入将不可见的选项卡项作为所选项的情况。您不需要这些。从概念上讲,TabControl
只是一个observedcollection
的图形表示,其中每个视图模型由一个选项卡项表示,并且在给定时间只有一个SelectedItem
。谢谢。我将致力于此和更新。代码似乎是我要找的。
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);
}
}
}
}