2015-02-23 32 views
0

我想在當前完全正常工作的Prism MVVM WPF應用程序中的TabControl的第一個標籤頁頭中顯示圖像。wpf中的第一個標籤頁頭中的圖像TabHeader

完整描述如下:

當用戶選擇從左側區域分類列表會顯示在右側區域「更多詳細信息」和「相關產品」的項目。此右側區域包含UserControl內的TabControl。 第一個標籤顯示「More category Details」,第二個標籤顯示「相關產品」。數據顯示正確。現在我只想在第一個選項卡標題中顯示類別縮略圖和類別名稱。

我嘗試的第一個選項卡上使用HeaderTemplate中如下

<TabControl VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" > 
<TabItem Name="tabItemCategoryMoreInfo" > 
    <TabItem.HeaderTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
     <Image x:Name="viewImage" Height="20" Width="20" Margin="0,0,2,0" 
         Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl} }, Path=Content.DataContext.SelectedParent.PictureBinary}"/> 
     <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Content.DataContext.SelectedParent.CategoryName}" 
          VerticalAlignment="Center" FontSize="14" FontWeight="SemiBold" /> 
     </StackPanel> 
     <DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Content.DataContext.SelectedParent.PictureBinary}" Value="{x:Null}" > 
      <Setter TargetName="viewImage" Property="Source" Value="/CatalogModule;component/Images/ItemIcon.png" /> 
     </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
    </TabItem.HeaderTemplate> 

    <ContentControl prism:RegionManager.RegionName="CategoryMoreDetailsRegion" /> 
</TabItem> 
<TabItem Header="Products" Name="tabItemCategoryProducts"> 
    <ContentControl prism:RegionManager.RegionName="CategoryProductsRegion" /> 
</TabItem> 

它沒有顯示產品名稱或產品形象。但它只顯示默認圖像,所以觸發器看起來工作。有些人可以幫忙嗎?

編輯:

最初我用的TabItem的TabControl的,而不是在圖像數據通道:

<Image x:Name="viewImage" Height="20" Width="20" Margin="0,0,2,0" 
        Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem} }, Path=Content.DataContext.SelectedParent.PictureBinary}"/> 

回答

0

Path在你DataTemplate Binding s爲不同的...這可能解釋爲什麼它不起作用。如果我理解正確的話,你說,在DataTrigger作品Binding Path,所以可能改變你的Binding PathImageSourceTextBlock.Text性能威力工作?:

<DataTemplate> 
    <StackPanel Orientation="Horizontal"> 
    <Image x:Name="viewImage" Height="20" Width="20" Margin="0,0,2,0" 
        Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=Content.DataContext.SelectedParent.PictureBinary}"/> 
    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Content.DataContext.SelectedParent.CategoryName}" 
         VerticalAlignment="Center" FontSize="14" FontWeight="SemiBold" /> 
    </StackPanel> 
    <DataTemplate.Triggers> 
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Content.DataContext.SelectedParent.PictureBinary}" Value="{x:Null}" > 
     <Setter TargetName="viewImage" Property="Source" Value="/CatalogModule;component/Images/ItemIcon.png" /> 
    </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 

萬一你不能看到其中的差別,你使用這個在你的Image.Source Binding.Path

AncestorType={x:Type TabControl} 
+0

最初我用TabItem而不是TabControl,我忘了它的內涵,它沒有工作。對不起,我沒有解釋清楚。我的意思是「dataTrigger正在工作」是因爲它無法從數據源中找到正確的圖片(因爲我的代碼不正確),它顯示文件夾中的默認圖片「ItemIcon.png」。 – Dush 2015-02-23 21:40:18