因此,为了保持简单的问题,我需要例如几十次使用这样的东西;ContentControl中嵌入的交互触发器
<Rectangle>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<ei:ChangePropertyAction TargetName="AnotherObjectOnTheView"
PropertyName="Visibility"
Value="Visible" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
除了显然我不想粘贴那几十遍,我需要它。所以我试图把它们放在ContentControl
里,像这样;
<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>
有了这个想法,我可以通过调用模板来替代每个实例的所有内容;
<ContentControl Style="{StaticResource MyThingy}"/>
除了问题,嵌入ContentControl
时,相互作用的触发器不会出现断火。它会显示模板项目,但似乎忽略触发器?
所以问题是,为什么附加到模板项目的触发器被忽略,或者,有没有更好的方法来实现我想要的?
谢谢,但我不是想让'鼠标单击'上的'Rectangle'消失,我试图让相同视图上的另一个对象变为'ControlTemplate'外的'Visible'(因此'TargetName =“AnotherObjectOnTheView”'),所以说我有一个对象与名为'AnotherObjectOnTheView'的样式模板相同的视图,我想要点击几十个地方使其成为可见的,这是矩形和触发器的来源。我尝试了TargetObject,但它似乎没有使“AnotherObjectOnTheView”可见:/ –
@ChrisW。您的问题是_“为什么附加到模板化项目的触发器被忽略”_答案是 - 它们不是,您没有使用'ChangePrope rtyAction'正确,正如我在答案中所展示的那样。你的新问题类似于_“我可以使用TargetName属性更改另一个控件的属性,并且答案是* no *,这是不能完成的。我建议像'EventToCommand'这样的东西来触发ViewModel的改变。例如ViewObject中的'AnotherObjectOnTheView'''Visibility'将绑定到的一个bool属性。 – Blachshma
或者直接使用'ChangePropertyAction'来更改ViewModel中的属性(通过绑定到MainWindow/UserControl的DataContext) – Blachshma