嵌入在ContentControl中的交互触发器

编程入门 行业动态 更新时间:2024-10-28 04:17:39
本文介绍了嵌入在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>

显然,除了我不想在需要的地方粘贴数十次。所以我试图将它们放入 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 ,而是在调用它们,这是有问题的 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中的交互触发器

本文发布于:2023-11-06 04:54:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   ContentControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!