问题
I have the following code:
<TabItem Name="tabItemGeneral">
<TabItem.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tabGeneralImg" Property="Source" Value="Image/tabGeneralActive.png"></Setter>
</Trigger>
</TabItem.Triggers>
<TabItem.Header>
<StackPanel Orientation="Horizontal" Margin="0,-3,0,0">
<Image Name="tabGeneralImg" Source="Images/tabGeneral.png" Width="11" Height="11"></Image>
<Label Name="tabGeneralLbl" Content="General"></Label>
</StackPanel>
</TabItem.Header>
<Grid>
<!-- ... -->
</Grid>
</TabItem>
but I get Error 1 'IsSelected' member is not valid because it does not have a qualifying type name. C:\MyNET\bluetooth\iLockWPF\iLock\iLock\MainWindow.xaml 63 60 iLock
during compilation. Why?
回答1:
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>
回答2:
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.
回答3:
Do you need to use TabItem.IsSelected?
<Trigger Property="TabItem.IsSelected" Value="True">
<Setter TargetName="tabGeneralImg" Property="Source" Value="Image/tabGeneralActive.png">
</Setter>
</Trigger>
来源:https://stackoverflow.com/questions/6009383/isselected-property-of-wpf-tabitem