我遇到了WPF事件触发器的问题:
在我的xaml中我得到了一个箭头:
<Path Data="{Binding Path=Points, Converter={StaticResource ResourceKey=pointCollectionConverter}}"
Stroke="Black"
MinWidth="1"
MinHeight="1"
Name="arrowPath"
StrokeThickness="2">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding Path=ArrowPathMouseLeftButtonDownCommand}"
PassEventArgsToCommand="True"></cmd:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="MouseRightButtonDown">
<cmd:EventToCommand Command="{Binding Path=ArrowPathMouseRightButtonDownCommand}"
PassEventArgsToCommand="True"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Path>
事件正在按预期触发,一切都很好。现在我得到了一些Ellypses
(在同一个xaml中),它应该像鼠标右键单击箭头一样。
所以我在我的xaml中添加了Ellypses触发器部分:
<ItemsControl ItemsSource="{Binding Points}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controls:DragCanvas AllowDragOutOfView="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Cursor="Hand"
Fill="Black"
Stroke="Black"
StrokeThickness="2"
Width="10"
Height="10"
Visibility="{Binding Visible, Converter={StaticResource ResourceKey=endPointTrimmer}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonDown">
<cmd:EventToCommand Command="{Binding Path=ArrowPathMouseRightButtonDownCommand}"
PassEventArgsToCommand="True"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left"
Value="{Binding X, Mode=TwoWay}" />
<Setter Property="Canvas.Top"
Value="{Binding Y, Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
但是我收到了绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'ArrowPathMouseRightButtonDownCommand' property not found on 'object' ''ConnectionPoint' (HashCode=54168362)'. BindingExpression:Path=ArrowPathMouseRightButtonDownCommand; DataItem='ConnectionPoint' (HashCode=54168362); target element is 'EventToCommand' (HashCode=42719917); target property is 'Command' (type 'ICommand')
我不明白为什么。请任何人帮助我
答案 0 :(得分:1)
你必须使用Ancestor Relative Source Binding。 喜欢
<cmd:EventToCommand Command="{Binding Path=ArrowPathMouseRightButtonDownCommand,RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType=Path}}"
PassEventArgsToCommand="True"></cmd:EventToCommand>
答案 1 :(得分:1)
为了获得与ItemsControl
相同的ViewModel(即数据上下文)的访问权限,您必须为您的商品控制一个名称,例如像这样:
<ItemsControl x:Name="itemsCtrl" ...>
然后您可以使用以下绑定来访问ViewModel中的ArrowPathMouseRightButtonDownCommand
属性:
{Binding Path=DataContext.ArrowPathMouseRightButtonDownCommand, ElementName=itemsCtrl}