0
votes

I am trying to create a ListView with ContextMenu inside of a WebBrowser and then use ICommand to Bind. However, the example listed below when this is ran I am getting the following error: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListView', AncestorLevel='1''. BindingExpression:Path=DataContext.MyCode; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

Here is my code: XAML

        <!--WebBrowser to Display Messages-->
        <WebBrowser Name="webBrowser"
                    Source="http://www.whatever.com" 
                   <!--OnContextMenuOpening event in LoadCompleted-->                       
                    LoadCompleted="webBrowser_LoadCompleted">

            <WebBrowser.Resources>
                <!--Show items on webBrowser on mouserightclick-->
                <ListView x:Key="wbListView">
                    <ListView.Resources>
                        <ContextMenu x:Key="wbContextMenu" >
                            <MenuItem Header="Test" CommandParameter="{Binding}" Command="{Binding Path=DataContext.myCode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"/>
                        </ContextMenu>
                    </ListView.Resources>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="{x:Type ListViewItem}">
                            <Setter Property="ContextMenu" Value="{StaticResource wbContextMenu}"/>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>
            </WebBrowser.Resources>

        </WebBrowser>

Here is my code:C#

    public myCodeCommand myCode
    {
        get { return new myCodeCommand(this); }
    }

    public class myCodeCommand : ICommand
    {           
        public bool CanExecute(object parameter)
        {
            Console.WriteLine("WEBBROWSER SEND MESSAGE CAN EXECUTE?");
            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            Console.WriteLine("WEBBROWSER SEND MESSAGE EXECUTE SOMETHING");
        }
    }
1

1 Answers

0
votes

I was able to create the ContextMenu inside Xaml as follow:

     <WebBrowser.ContextMenu>
          <ContextMenu x:Name="wbContextMenu" >
               <MenuItem x:Name="menuItemOne" Header="Item 1" Click="menuItemOne_Click" />
               <MenuItem x:Name="menuItemTwo" Header="Item 2" Click="menuItemTwo_Click" />
          </ContextMenu>
     </WebBrowser.ContextMenu>

and on the wbContextMenu event handler as follows: fyi each menu item has their own clickevent.

    DocumentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;

    private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
    {
           wbContextMenu.PlacementTarget = pEvtObj as ContextMenu;
           wbContextMenu.IsOpen = true;
     }

    private void menuItemOne_Click(object sender, RoutedEventArgs e)
    {
           //Put whatever you want handled here for menuitem
    }

    private void menuItemTwo_Click(object sender, RoutedEventArgs e)
    {
           //Put whatever you want handled here for menuitem
    }