Diego Silva Pires
Diego Silva Pires

Reputation: 291

WPF Caliburn.Micro ItemsControl ContextMenu don't find method

I'am really having some problems with the ContextMenu from Caliburn.Micro. No matter what I do, I get the error "Cant' find the method ...".

I've tried this solutions, and that one too, but I can't get this to work.

What I'am doing wrong? Actually, there's a way to debug the view and find out in which Context the control is searching for the respective ViewModel?

And another thing... this View is inside a DataTemplate from another view, does that change anything? All the others bindings are working from the correct ViewModel...

<ScrollViewer VerticalScrollBarVisibility="Auto"  
                  HorizontalScrollBarVisibility="Disabled" 
                  Grid.Row="1" Grid.Column="0" 
                  Padding="10,5,15,5"
                  MaxHeight="390" x:Name="xImages">
        <ItemsControl 
            ItemsSource="{Binding Path=Document.Images}"                
            dd:DragDrop.IsDragSource="True"
            dd:DragDrop.IsDropTarget="True">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="110">
                        <Border Margin="5" BorderBrush="Gainsboro" BorderThickness="1">
                            <Image Source="{Binding Path=PathThumb}" Width="70" Height="100"> <!-- Tag="{Binding DataContext, ElementName=xImagens}" -->
                                <Image.ContextMenu>
                                    <ContextMenu 
                                        cal:Action.TargetWithoutContext="{Binding DataContext, ElementName=xImagens}"> <!--PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"-->
                                        <MenuItem Header="Ampliar"
                                                cal:Message.Attach="[Event Click] = [Action ExpandImage($datacontext)]"></MenuItem>
                                        <MenuItem Header="Excluir" 
                                                cal:Message.Attach="[Event Click] = [Action DeleteImage($datacontext)]"></MenuItem>
                                    </ContextMenu>
                                </Image.ContextMenu>
                            </Image>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </ScrollViewer>

Upvotes: 2

Views: 2286

Answers (2)

eran otzap
eran otzap

Reputation: 12533

 cal:Action.TargetWithoutContext="{Binding Source={x:Reference xImagens} , Path=DataContext}">

Because ContextMenu is not part of the VisualTree , you can't bind to elements in the visual tree of it's TargetPlacement. ( Though in XAML it seems like it is part of it ).

You have two choices:

   {Binding Path=PlacementTarget.DataContext}

Or:

   {Binding Source={x:Reference xImagens}, Path=DataContext}

Upvotes: 4

mvermef
mvermef

Reputation: 3914

give the x:Name="xImages" to the ItemsControl and then do the binding with ElementName to the ContextMenu using the Action.TargetWithoutContext, you had it right but the ScrollViewer isn't what has the Datacontext to the List of data, the ItemControl does since it has the ItemSource.

Was there a reason for naming the ScrollViewer?

<ItemsControl x:Name="xImages">

<ContextMenu Action.TargetWithoutContext="{Binding Path=DataContext, ElementName=xImages}">
<!-- Shortened -->
</ContextMenu>

</ItemsControl>

Upvotes: 1

Related Questions