0
votes

I'm having trouble with binding a ContextMenuItem's command to my parent object. I've followed the following examples:

And I've got a lot closer, but I still get the following error:

System.Windows.Data Error: 40 : BindingExpression path error: 'SearchString' property 
not found on 'object' ''MainWindow' (Name='root')'. 
BindingExpression:Path=Parent.PlacementTarget.Tag.SearchString; DataItem='MenuItem' 
(Name=''); target element is 'MenuItem' (Name=''); target property is 'Command' (type 
'ICommand')

The main window class has SearchString defined as:

public partial class MainWindow : Window
{
    ...

    private void SearchString(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
}

but, obviously, the exception is never getting thrown. I have the menu defined in a DataTemplate as follows:

<Window x:Class="CodeNaviWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
        Title="View" Height="865" Width="991"
        x:Name="root"
    >
    <Window.Resources>
        <DataTemplate x:Key="fileContentView">
            <StackPanel>
                <Border BorderThickness="3" BorderBrush="BurlyWood">
                    <avalonEdit:TextEditor 
                        Width="400" 
                        Height="400" 
                        Document="{Binding Path=Document}" 
                        IsReadOnly="True"
                        Tag="{Binding ElementName=root}">
                        <avalonEdit:TextEditor.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                <MenuItem Header="Search..." Command="{Binding Path=Parent.PlacementTarget.Tag.SearchString, RelativeSource={RelativeSource Self}}" />
                            </ContextMenu>
                        </avalonEdit:TextEditor.ContextMenu>
                    </avalonEdit:TextEditor>
                </Border>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
...
</Window>

Can anyone see where I'm going wrong? If I change the method to be a string property then I don't get any errors, so I'm guessing that I'm somehow telling the XAML to expect a property, rather than a method.

1

1 Answers

0
votes

Answering my own question here but hopefully this will prove useful for others. The solution that worked for me was to follow the answers given here: How do I add a custom routed command in WPF?

My MainWindow now looks like the following:

namespace MyNamespace
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            ...
        }

        ...

        private void SearchString(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }

    public static class Commands
    {
        public static readonly RoutedUICommand SearchString = new RoutedUICommand("Search String", "SearchString", typeof(MainWindow));
    }
}

And the XAML has the following additions:

<Window x:Class="CodeNaviWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CodeNaviWPF"
        xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
        Title="MyApp" Height="865" Width="991"
        x:Name="root"
    >
    <Window.CommandBindings>
        <CommandBinding Command="local:Commands.SearchString" Executed="SearchString" />
    </Window.CommandBindings>

    <Window.Resources>
        <DataTemplate x:Key="fileContentView">
            <StackPanel>
                <Border BorderThickness="3" BorderBrush="BurlyWood">
                    <avalonEdit:TextEditor 
                        Width="400" 
                        Height="400" 
                        Document="{Binding Path=Document}" 
                        IsReadOnly="True"
                        Tag="{Binding ElementName=root}">
                        <avalonEdit:TextEditor.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                <MenuItem Header="Search..." Command="local:Commands.SearchString" />
                            </ContextMenu>
                        </avalonEdit:TextEditor.ContextMenu>
                    </avalonEdit:TextEditor>
                </Border>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    ...
</Window>