I've got the following TabItem
template where the TabItem
contains an image whose source depends on the IsSelected
property. I accomplish this by binding the Image.Source
to the TabItem.Header
, with a Converter
that converts the header text to a full filename. (ie a header of "awb" will become "images/awb-white.png" or "images/awb-black.png" depending on the converter).
This code works, but it seems to have some unnecessary redundancy. All I really need to change is the Image.Source
's binding's converter; not the whole binding itself. (The RelativeSource and Path both stay the same). Is there any way to accomplish this without the redundancy?
<Window.Resources>
<local:UnselectedImageFilenameConverter x:Key="UnselectedImageFilenameConverter" />
<local:SelectedImageFilenameConverter x:Key="SelectedImageFilenameConverter" />
<ControlTemplate TargetType="TabItem" x:Key="TabItemTemplate">
<Image x:Name="TabImage" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header, Converter={StaticResource UnselectedImageFilenameConverter}}" Stretch="None"/>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="TabImage" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header, Converter={StaticResource SelectedImageFilenameConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<TabControl>
<TabItem Header="awb" Template="{StaticResource TabItemTemplate}">
<Grid/>
</TabItem>
<TabItem Header="av" Template="{StaticResource TabItemTemplate}">
<Grid/>
</TabItem>
</TabControl>
To my knowledge that is not possible.
But you could change the way you handle this by creating only one converter but passing both the original value and the selection status via a MultiBinding
. (The converter would need to be an IMultiValueConverter
). Whether that is a good idea is of course questionable...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.