IsSelected property of WPF TabItem

烈酒焚心 提交于 2019-12-05 05:51:48
Marat Khasanov

As CodeNaked said, it is unable to use property triggers directly on elements. You should use HeaderTemplate instead.

<TabControl>
    <TabControl.Resources>
        <DataTemplate x:Key="tabItemGeneralHeaderTemplate">
            <StackPanel Orientation="Horizontal" 
                        Margin="0,-3,0,0">
                <Image Name="tabGeneralImg" 
                       Source="Image/tabGeneralActive.png" 
                       Width="11" 
                       Height="11"/>
                <Label Name="tabGeneralLbl" 
                       Content="General"/>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected
                                ,RelativeSource={RelativeSource TemplatedParent}}" 
                             Value="True">
                    <Setter TargetName="tabGeneralImg" 
                            Property="Source" 
                            Value="Images/tabGeneral.png"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </TabControl.Resources>
    <TabItem Name="tabItemGeneral" 
             HeaderTemplate="{StaticResource tabItemGeneralHeaderTemplate}">
        <Grid>
            ...
        </Grid>
    </TabItem>
</TabControl>

The TabItem.Triggers collection is inherited from FrameworkElement and can only contain EventTriggers. What you have is not valid.

Triggers, MultiTriggers, DataTriggers, and MultiDataTriggers can only be used in the Style.Triggers, ControlTemplate.Triggers, or DataTemplate.Triggers collections.

Do you need to use TabItem.IsSelected?

<Trigger Property="TabItem.IsSelected" Value="True">
  <Setter TargetName="tabGeneralImg" Property="Source" Value="Image/tabGeneralActive.png">
  </Setter>
</Trigger>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!