因此,为了使问题简单,例如,我需要使用类似的方法;
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>显然,除了我不想在需要的地方粘贴数十次。所以我试图将它们放入 ContentControl 中,像这样;
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}"/>问题在于,当嵌入到 ContentControl ,则不会触发互动触发器。
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?
推荐答案不是 Interaction.Triggers 没有调用code>,而是在调用它们,这是有问题的 ChangePropertyAction 。
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>请注意,我所做的唯一更改是1.将矩形设为红色(这样您可以更好地看到它何时消失); 2.单击按钮,立即使触发器隐藏矩形。
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.
那我的代码为什么起作用?因为我不是使用 TargetName ,而是使用 TargetObject 并绑定到模板化父对象。您不能通过名称定位模板中的元素,这是一个不同的名称范围,据我所知,TargetName根本无法在样式中使用,只能在ControlTemplate.Triggers
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
更多推荐
嵌入在ContentControl中的交互触发器
发布评论