0

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 に何らかの方法で指示していると推測しています。

4

1 に答える 1

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>
于 2013-07-11T13:50:57.973 に答える