Interaction Triggers Embedded in ContentControl

大兔子大兔子 提交于 2020-02-03 09:01:07

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/14247877/interaction-triggers-embedded-in-contentcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!