2016-09-29 101 views
-2

有TabControl,它綁定到數組,需要添加到TabItem不同的圖標/圖像。 HAML代碼:WPF,TabItem圖像

<Grid> 
     <TabControl VirtualizingPanel.VirtualizationMode="Recycling" Style="{StaticResource TabControl}" 
     ItemsSource="{Binding Workspaces}" SelectedIndex="{Binding CurrentPage,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"> 
     <TabControl.ItemContainerStyle> 
      <Style BasedOn="{StaticResource TabItem}" TargetType="{x:Type TabItem}"> 
       <Setter Property="Header" Value="{Binding HeaderText}"/> 
      </Style> 
     </TabControl.ItemContainerStyle> 
     <TabControl.ContentTemplate> 
      <DataTemplate> 
       <ContentPresenter Content="{Binding Content}" /> 
      </DataTemplate> 
     </TabControl.ContentTemplate> 
    </TabControl> 
</Grid> 
+1

什麼題?你到目前爲止嘗試了什麼?發佈您的代碼!當你運行它時發生了什麼?你預期會發生什麼?你有什麼特別的問題? – Robert

回答

0

你應該在你的TabControl設置ItemTemplateSelector:

<TabControl VirtualizingPanel.VirtualizationMode="Recycling" Style="{StaticResource TabControl}" 
    ItemsSource="{Binding Workspaces}" SelectedIndex="{Binding CurrentPage,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" 
    ItemTemplateSelector="{StaticResource myItemDataTemplateSelector}" > 

你TemplateSelector應該是這樣的:

public class MyItemDataTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     FrameworkElement element = container as FrameworkElement; 

     if (element != null && item != null && item is MyItem) 
     { 
      var myItem = item as MyItem; 
      var window = Application.Current.MainWindow; 

      switch (myItem.SpecialFeatures) 
      { 
       case SpecialFeatures.None: 
        return 
         element.FindResource("Item_None_DataTemplate") 
         as DataTemplate; 
       case SpecialFeatures.Color: 
        return 
         element.FindResource("Item_Color_DataTemplate") 
         as DataTemplate; 
      } 
     } 

     return null; 
    } 
} 

參考:https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplateselector(v=vs.110).aspx