Dax Fohl
Dax Fohl

Reputation: 10781

Is it possible to change a the converter of a binding in a trigger in WPF?

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. (i.e. 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>

Upvotes: 0

Views: 247

Answers (1)

brunnerh
brunnerh

Reputation: 184516

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

Upvotes: 2

Related Questions