我有一个CustomControl的以下XAML:
<Style TargetType="{x:Type itvw:ItemView}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itvw:ItemView}">
<Border>
<Grid>
<TextBox/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--triggers-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想使用Interactivity / EventToCommand捕获整个CustomControl的KeyDown事件。
类似的东西:
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding KeyDownCommand, Mode=OneWay}" PassEventArs="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
我的问题是 - 我在哪里将触发器放在ControlTemplate标记中?我想在后面的代码中做相同的事情:
this.KeyDown += ItemView_KeyDown;
答案 0 :(得分:1)
您可以将它们放在ControlTemplate中或Border元素中:
<Style TargetType="{x:Type itvw:ItemView}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itvw:ItemView}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding KeyDownCommand, Mode=OneWay}" PassEventArs="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Border>
<Grid>
<TextBox/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--triggers-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type itvw:ItemView}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itvw:ItemView}">
<Border>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding KeyDownCommand, Mode=OneWay}" PassEventArs="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<TextBox/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--triggers-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>