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");
}
}