2015-11-02 50 views
0

我有一個應用程序,名爲應用程序窗口。我需要在選項卡中顯示已安裝的應用程序。每個選項卡項都包含有關其相應應用程序的數據。如果安裝了新的應用程序,那麼窗口將爲新安裝的應用程序創建一個新的選項卡項目。根據WPF中選中的tabite顯示內容

*我已經完成了根據安裝的應用程序創建一個新的選項卡項目。

代碼我使用:

private List<TabItem> _tabItems; 
private TabItem _tabAdd; 

try 
{ 
    _tabItems = new List<TabItem>(); 

    InstalledApps.DataContext = _tabItems; 

    InstalledApps.SelectedIndex = 0; 

    if (A Installed) 
     this.AddTabItem("A"); 

    if (B Installed) 
     this.AddTabItem("B"); 
} 
catch (Exception ex) 
{ 
} 


private TabItem AddTabItem(string AppName) 
{ 

    int count = 1; 

    // create new tab item 
    TabItem tab = new TabItem(); 

    tab.Header = AppName; 
    string tabName = AppName.ToLower(); 
    tab.Name = tabName; 
    tab.HeaderTemplate = InstalledApps.FindResource("TabHeader") as DataTemplate; 

    TextBox txt = new TextBox(); 
    txt.IsReadOnly = true; 

    tab.Content = txt; 

    _tabItems.Insert(count - 1, tab); 

    return tab; 
} 

的XAML:

<TabControl Height="555" HorizontalAlignment="Left" Margin="0,-27,0,0" Name=" InstalledApps " ItemsSource="{Binding}" BorderThickness="0,0,0,0" SelectionChanged=" InstalledApps_SelectionChanged" VerticalAlignment="Top" Width="992" Grid.ColumnSpan="3" Grid.RowSpan="4"> 
    <TabControl.Resources> 
     <Style TargetType="TabItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="TabItem"> 
         <Grid Name="Panel"> 
          <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" 
           HorizontalAlignment="Center" ContentSource="Header" Margin="80,10" /> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter TargetName="Panel" Property="Background" Value="#FFEFEFEF" /> 
          </Trigger> 
          <Trigger Property="IsSelected" Value="False"> 
           <Setter TargetName="Panel" Property="Background" Value="#FFE0E0E0" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0"> 
        <Setter Property="Visibility" Value="Hidden" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
     <DataTemplate x:Key="TabHeader" DataType="TabItem"> 
      <DockPanel> 
       <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" /> 
      </DockPanel> 
     </DataTemplate> 

    </TabControl.Resources> 
</TabControl> 

我的要求是: 雖然我從列出的選項卡項目中選擇任何標籤,那麼它應該在其內容部分顯示其相應的內容(標識,應用名稱)。

由於提前, Kathiresan S.

+0

你看到TabControl的任何項目,當你運行你的應用程序? – user1672994

+0

是的。我的應用程序在應用程序窗口中顯示選項卡項目A和B. –

回答

1

創建表示要顯示的數據的類。 e.g:

public class AppDetails { 
    public Uri Logo { get; set; } 
    public string AppName { get; set; } 
    //etc 
} 

然後設置AppDetails到TabItem.Content和定義TabItem.ContentTemlate類似於你HeaderTemplate中一樣。

tab.ContentTemplate = InstalledApps.FindResource("TabContent") as DataTemplate; 
tab.Content = new AppDetails{ ... }; 

或者,你可以跳過的ContentTemplate和分配一些FrameworkElement的(例如自定義用戶控件)直接到選項卡的內容:

tab.Content = new AppDetailsUserControl(AppName, Logo); 
1

答到如果安裝了新的應用程序,那麼窗口將新安裝的應用程序創建一個新的選項卡項目。

  1. 應的TabControl的的DataContext綁定到的ObservableCollection,這樣任何由後端增加了新的TabItem將直接顯示UI。

答到雖然我從列出的選項卡的項目中選擇任何選項卡,那麼它應該顯示其相應的內容(標識,應用程序的名稱)中是內容部分

  • 您應該將內容tab.Content設置爲UserControl或Model(如果Model然後在ResourceDictionary中定義DataTemplate)。 UserControl或DataTemplate可以定義應該在什麼控件及其視覺層次結構中顯示哪些信息。