2013-03-04 162 views
4

我一直在閱讀的Visibility.Collapsed。當Visibility設置爲Collapsed時,TabItem標題被隱藏,但內容仍然可見。處理WPF表項可見性屬性

我也嘗試過在here中提到的以下方法,但沒有運氣。

有什麼方法可以讓TabItems中的內容隱藏,並且還可以選擇可見的選項卡。

+1

你不需要這些。從概念上講,一個'TabControl'只是一個'ObservableCollection '的圖形表示,其中每個視圖模型由一個標籤項表示,並且在給定時間只有1個'SelectedItem'。 – 2013-03-04 16:13:18

回答

2

嗨只是添加和刪除TabControl的TabItems,而不是設置可見性。打開和關閉可見性還有一個問題,當您滾動或最小化或調整TabControl的大小時,它就像Out of index一樣是個例外。

6

你不需要這些。從概念上講,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爲你的標籤中的每一個問題(CREA在每個內部都有適當的邏輯),並且在app.xaml中的這些派生類中的每個派生類都有一個合適的DataTemplate或其他東西。

無論何時您想從視圖中刪除選項卡項目,而不是操縱視圖來操作ViewModel。這是WPF的一切方法。它通過消除代碼中操作複雜對象(UI元素)的需求來簡化所有操作。只要設置

TabbedViewModel.SelectedItem.IsVisible = false;,確保你也這樣做:

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

這將阻止你落入具有與所選擇的項目一個不可見的選項卡項目的情況。

+0

謝謝。我將致力於此並進行更新。該代碼似乎是我在尋找的。 – aioracle 2013-03-05 00:11:28