选择tabitem时设置图像

时间:2012-01-08 19:57:54

标签: wpf visual-studio-2010 xaml tabcontrol

我是wpf的新手,我无法弄清楚如何在选择标签项时设置图像。

我写了一个风格,但它不起作用。它向我展示了任何东西......

<Style TargetType="TabItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <Grid>
                   <StackPanel Orientation="Horizontal">
                       <HeaderedItemsControl Name="img"  >
                       </HeaderedItemsControl>
                   </StackPanel>
                </Grid>
             <ControlTemplate.Triggers>
             <Trigger Property="IsSelected" Value="True">
                   <Setter Property="Visibility" TargetName="img" Value="Visible"/>    
             </Trigger>      
             <Trigger Property="IsSelected" Value="False">
                   <Setter Property="Visibility" TargetName="img" Value="Hidden"/>
             </Trigger>
         </ControlTemplate.Triggers>      
       </ControlTemplate>     
     </Setter.Value>      
   </Setter>     
 </Style>     

对于选项卡控件,我编写了代码:

 <TabControl Name="tabControl1"  
             HorizontalAlignment="Left"                  
             VerticalAlignment="Top" 
             Height="311" 
             Width="503">
    <TabItem Name="tabItem1" Background="Red"  >                
            <TabItem.Header>
                <Grid>
                    <TextBlock Text="   First" Width="100" Margin="-2,33,4,16" />
                    <Image  Name="img" Source="menu.png" Height="20" Margin="0,60,0,0" />
                </Grid>

            </TabItem.Header>
            <Grid>

            <TextBox Name="denumire" Margin="31,20,169,67" Height="20" Width="60"/>
            </Grid>

        </TabItem>
    <TabItem Name="tabItem2" Header="tabItem2" Background="Red">
        </TabItem>
    </TabControl>

1 个答案:

答案 0 :(得分:0)

按照您的风格,将HeaderedItemsControl更改为以下内容:

<HeaderedItemsControl>
    <TextBlock Text="{TemplateBinding Header}" Width="100"/>
    <Image x:Name="img" Source="menu.png" Height="20" />
</HeaderedItemsControl>

并删除<TabItem.Header>中的整个TabControl部分。