开发者

IsSelected property of WPF TabItem

开发者 https://www.devze.com 2023-03-06 13:33 出处:网络
I ha开发者_如何学JAVAve the following code: <TabItem Name=\"tabItemGeneral\"> <TabItem.Triggers>

I ha开发者_如何学JAVAve 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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消