Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsSelected property of WPF TabItem

Tags:

wpf

tabitem

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?

like image 998
Oleg Sh Avatar asked May 15 '11 15:05

Oleg Sh


2 Answers

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>
like image 119
Marat Khasanov Avatar answered Nov 04 '22 16:11

Marat Khasanov


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.

like image 4
CodeNaked Avatar answered Nov 04 '22 18:11

CodeNaked