2

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>
Community
  • 1
  • 1

2 Answers2

4
 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}
Napoleon
  • 340
  • 3
  • 13
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • with {x:Reference xImagens, Path=DataContext} it doesn't reconize the markdown... the other solution doesn't work either... – Diego Silva Pires Sep 01 '14 at 19:58
  • @DiegoSilvaPires You can remove the extra closing brace at the end of the quote and the markup should be good. This approach did the trick for me. – Jeremy Jan 28 '15 at 21:27
1

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>
mvermef
  • 3,814
  • 1
  • 23
  • 36
  • I've tried putting the Name in the ItemsControls, but it didnt worked, so I've imagined that maybe in the itemscontrols the context used was from my list, but I want the context from the VM... It didnt worked that way too... – Diego Silva Pires Sep 01 '14 at 21:27
  • try this... cal:Binding.Model="{Binding}" along with the above markup, it would help to see a little more of your view in its construction... If you can – mvermef Sep 02 '14 at 17:14
  • Where should I put it? Tried in the ItemsControls, and in the ContextMenu, but neither worked – Diego Silva Pires Sep 02 '14 at 18:34
  • Where does the control fall in the datatemplate, with respect to the depth of DataTemplates this goes into? If you want the viewmodel that this particular control is bound to, to actually get the viewmodel that generated it would probably go on the ScrollViewer but without seeing how you are composing the view, I can't be certain it will work at all. Basically you are bumping on a limitation of VisualTree and DataTemplates with respect to CM's ability to bind correctly using convention. – mvermef Sep 02 '14 at 18:45
  • I have a MetroWindow with multiple view as DataTemplates, rendering inside a ContentControl... every view is a UserControl, and I have just this ScrollViewer and some Buttons inside a Grid... I'am probably breaking some rule of convention, but dont don't know where... there isn't any way to discover in which Context the binding are looking for the method? Strange enough is that the buttons outside the ItemsControls work properly, – Diego Silva Pires Sep 02 '14 at 20:30
  • LogManager.GetLog => type = new Debugger(type); in the Constructor of the boot strapper assuming v2.x Debugger is baked in where in previous releases you had to create your own. LogManager.GetLog was always there. Everything depends on how you have ViewModels called, with respect to bindings assuming they are found with associated view. – mvermef Sep 03 '14 at 03:14
  • Ok, after a while I've came back to the problem... resolved now, don't know what i was doing wrong the last time... anyway the debugger was my savior, thanks man – Diego Silva Pires Sep 16 '14 at 18:01