Minato
Minato

Reputation: 352

How To Apply Trigger To Image

I'm very new to WPF (and quite frankly I don't know why WinForms even exists because in my opinion it's FAR inferior to WPF), so I'm still not quite in the swing of things.

I have a TabControl, and inside each TabHeader is an image. Essentially, I just want the selected TabItem to have an Image with a gaussian blur radius of 2 and all the non-selected TabItems to have an Image with a gaussian blur of 8.

I've been looking through a lot of material on XAML, WPF, triggers, etc. and I'm just overwhelmed with information. Could someone help me out?

Upvotes: 0

Views: 59

Answers (1)

dkozl
dkozl

Reputation: 33384

You can achieve that by changing Effect on the image depending on TabItem.IsSelected. Lets say this is your Image in the Header

<Image Source="...">                        
   <Image.Style>
       <Style TargetType="{x:Type Image}">
           <Setter Property="Effect">
               <Setter.Value>
                   <BlurEffect Radius="8"/>
               </Setter.Value>
           </Setter>
           <Style.Triggers>
               <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="True">
                   <Setter Property="Effect">
                       <Setter.Value>
                           <BlurEffect Radius="2"/>
                       </Setter.Value>
                   </Setter>
               </DataTrigger>
           </Style.Triggers>
       </Style>
   </Image.Style>
</Image>

basically you create DataTrigger which binding goes up the visual tree to TabItem and trigger on IsSelected=true

Upvotes: 3

Related Questions