Activadores de interacción integrados en ContentControl

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, algo como esto;

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

preguntado el 09 de enero de 13 a las 22:01

1 Respuestas

No es que el Interaction.Triggers aren't being called - they ARE being called, it's the ChangePropertyAction lo cual es problemático.

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, Estoy usando 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

Respondido el 09 de enero de 13 a las 23:01

Thanks, but I'm not trying to make the Rectangle disappear on Mouse Click, I'm trying to make another object on the same view become Visible fuera de la ControlTemplate instead (hence the TargetName="AnotherObjectOnTheView") so say I have an object on the same view as the style template named 'AnotherObjectOnTheView" and I want dozens of places you can click to make it Visible which is where the Rectangles and Triggers come in. I tried the TargetObject but it doesnt seem to make "AnotherObjectOnTheView" visible :/ - Chris W.

@ChrisW. Your question was "Why are the triggers attached to the templated item getting ignored" which the answer is - They aren't, you're not using the ChangePropertyAction correctly as I showed in my answer. Your new question is something like "Can I change a property of another control from a Style binding using the TargetName attribute" y la respuesta es no, this cannot be done. I'd recommend something like a 'EventToCommand' to trigger a change in the ViewModel. For example a bool property in the ViewModel which the AnotherObjectOnTheView 's Visibility will bind to. - Blachshma

O utilice directamente el ChangePropertyAction to change that property in the ViewModel (by binding to the DataContext of the MainWindow/UserControl) - Blachshma

Ah ya, so now my questions answered in a couple ways. First I forgot the IsHitTestVisible=True (which is what Fill also accomplishes.) As well as the inability to interact with controls outside of the template via changeproperty without wiring into the VM. Thanks Blachshma! :) - Chris W.

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.