user3596113
user3596113

Reputation: 878

Event Triggers in WPF binding doesnt work

I got a problem with WPF Event Triggers:

in my xaml i got an arrow:

<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>

The Events are getting triggered as expected and everything is fine. Now i got some Ellypses (within the same xaml) which should behave just like the arrow on mouse right click.

So I added to my xaml for the Ellypses the Trigger part:

<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>

But I'm getting a binding error:

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')

and i dont understand why. Pleaseanyone help me

Upvotes: 0

Views: 1470

Answers (2)

user128300
user128300

Reputation:

In order to gain access to the same ViewModel (i.e. data context) as the ItemsControl, you have to give your items control a name, e.g. like this:

<ItemsControl x:Name="itemsCtrl" ...>

and then you can use the following binding to access the ArrowPathMouseRightButtonDownCommand property in the ViewModel:

{Binding Path=DataContext.ArrowPathMouseRightButtonDownCommand, ElementName=itemsCtrl}

Upvotes: 1

Dhaval Patel
Dhaval Patel

Reputation: 7591

you have to use Ancestor Relative Source Binding. like

<cmd:EventToCommand Command="{Binding Path=ArrowPathMouseRightButtonDownCommand,RelativeSource={RelativeSource
                    Mode=FindAncestor,
                    AncestorType=Path}}"
                                                PassEventArgsToCommand="True"></cmd:EventToCommand>

Upvotes: 1

Related Questions