Question

So to keep the question simple what I need for example is to use something like this dozens of times;

    <Rectangle>
        <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseLeftButtonDown">
                  <ei:ChangePropertyAction TargetName="AnotherObjectOnTheView"
                                           PropertyName="Visibility"
                                           Value="Visible" />                           
             </i:EventTrigger>                        
        </i:Interaction.Triggers>
   </Rectangle>

Except obviously I don't want to paste that dozens of times everywhere I need it. So I tried to plop them in a ContentControl, something like this;

<Style x:Key="MyThingy" TargetType="ContentControl">
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Rectangle>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonDown">
                            <ei:ChangePropertyAction TargetName="AnotherObjectOnTheView"
                                                     PropertyName="Visibility"
                                                     Value="Visible" />                                         
                        </i:EventTrigger>                        
                    </i:Interaction.Triggers>
                </Rectangle>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

With the idea I could substitute all of that per instance by just calling the template like;

<ContentControl Style="{StaticResource MyThingy}"/>

Except the problem is, when embedded in ContentControl, the Interaction Triggers don't appear to fire off. It will display the templated item fine, but seems to ignore the triggers?

So the question is, Why are the triggers attached to the templated item getting ignored, or, is there a better way to accomplish what I want?

Was it helpful?

Solution

It's not that the Interaction.Triggers aren't being called - they ARE being called, it's the ChangePropertyAction which is problematic.

For example, this will work fine:

    <Style x:Key="MyThingy" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl"> 
                    <Rectangle Fill="Red">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseLeftButtonDown">
                                <ei:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                                 PropertyName="Visibility"
                                                 Value="Collapsed" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Rectangle>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Notice the only changes I did were 1. Make the rectangle red (so you can better see when it disappears) and 2. Make the trigger hide the rectangle as soon as you click the button.

So why is my code working? Because instead of using TargetName, I'm using TargetObject and binding to the templated parent. You cannot target elements in the template via name, it's a different namescope, also as far as I recall TargetName doesn't work in Styles at all, only in ControlTemplate.Triggers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top