ContextMenuItem上的绑定命令

时间:2013-07-11 12:03:00

标签: c# .net wpf xaml binding

我在将ContextMenuItem命令绑定到父对象时遇到问题。我遵循了以下例子:

我离得更近了,但我仍然遇到以下错误:

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')

主窗口类的SearchString定义为:

public partial class MainWindow : Window
{
    ...

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

但显然,异常永远不会被抛出。 我在DataTemplate中定义了如下菜单:

<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>

谁能看到我哪里出错了?如果我将方法更改为字符串属性,那么我不会得到任何错误,所以我猜我在某种程度上告诉XAML期望属性,而不是方法。

1 个答案:

答案 0 :(得分:0)

在这里回答我自己的问题,但希望这对其他人有用。对我有用的解决方案是遵循这里给出的答案:How do I add a custom routed command in WPF?

我的MainWindow现在看起来如下:

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

XAML增加了以下内容:

<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>