0

ここで TreeView-Binding と ContextMenu に問題がありました: Selected TreeViewItem is null

今、私はこの問題を抱えています: ContextMenu があります

<TreeView.ContextMenu>
    <ContextMenu x:Name="MyContext" ItemsSource="{Binding OCContext}" DisplayMemberPath="Text"/>
</TreeView.ContextMenu>

http://i.stack.imgur.com/4gA1l.png

(画像は、私の ContextMenu がどのように見えるかを示しています。tabItem は気にしないでください...)。

ご覧のとおり、これは単なる ContetMenu であり、MenuItem ではありません! ユーザーが [閉じる] をクリックした場合、ViewModel で何かを実行したい (コマンドを発行しますか?)。また、彼がクリックしたボタン/メニューも知りたいです。ItemsSource がバインドされているため、メニューの量は動的です。

これは私のビューモデルです:

private ObservableCollection<T_Antwort> _occontext;
public ObservableCollection<T_Antwort> OCContext
{
    get
    {
        if (_occontext == null)
            _occontext = new ObservableCollection<T_Antwort>();
        return _occontext;
    }
    set
    {
        _occontext = value;
        RaisePropertyChanged(() => OCContext);
    }
}

したがって、ContextMenu (「アイテム」Close および CloseOtherThankThis) を ViewModel にバインドするだけでよいため、ユーザーがそれらのいずれかをクリックすると、ViewModel でそれらにアクセスしたいと考えています。これは、それらを 1 つずつバインドしたくないことを意味します。何らかの方法でイベント (ContextMenuItemClicked (?)) を取得し、これを ViewModel で使用したいと考えています。

ところで。ContextMenu の下で MenuItem を使用すると、別の「Menu フォルダー」が作成されるため、

" " -> 閉じる

" " -> CloseOtherThankThis

そして、私はそれをこのように見せたくありません。

編集:現在、次のようなアイテムを取得しています:

    private void MyContext_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        System.Windows.Controls.Primitives.MenuBase s = sender as System.Windows.Controls.Primitives.MenuBase;
        ItemCollection ic = s.Items;
        T_Antwort SelectedItem = (T_Antwort)ic.CurrentItem;
    }

選択したアイテムをバインディングで取得する可能性はありますか?

4

1 に答える 1

0

試したかどうかはわかりませんが、コンテキスト メニュー用の PlacementTarget があり、コンテキスト メニューを含むオブジェクトを提供します。

私が持っていたあるプロジェクトでは、次のようなものを作りました:

 <MenuItem ...    Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem
于 2013-07-11T11:42:57.153 に答える