调用 WPF 选择作为上下文菜单的 TreeView 项 [英] WPF selection of a TreeView item as context menu is called

查看:34
本文介绍了调用 WPF 选择作为上下文菜单的 TreeView 项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题主要与上下文菜单有关,但在我的特定情况下,它与 TreeView 控件有关.

This problem relates mainly to context menus, but in my specific case it's about a TreeView control.

TreeView 项包含一个 StackPanel,在该 StackPanel 上是一个 ContextMenu 属性,我已将其分配给 StaticResource(当然是 ContextMenu).所述 ContextMenu 导致 ICommand 并因此完成其任务.

The TreeView item contains a StackPanel, and on that StackPanel is a ContextMenu property, which I have assigned to a StaticResource (which is a ContextMenu of course). Said ContextMenu leads to an ICommand and, thus, does its thing.

目前(这是我相信的默认行为),右键单击 TreeView 中的项目不会选择该项目.这在 Windows 中很常见,但在这里不会发生.我希望它发生(但我不知道如何).

At present (and this is the default behaviour I believe), right clicking on an item in the TreeView does not select that item. This is common in Windows, but doesn't happen here. I would like it to happen (but I don't know how).

一些后续信息:我在 TreeView 中有一个选定的项目,单击鼠标左键即可更改.不过,这不是左键单击事件,而是SelectedItemChanged"事件.这导致了一种方法,我将数据上下文(视图模型)中的SelectedItem"设置为 SelectedItem.必须这样做,因为 TreeView 的选定项是只读"的.

A little follow up information: I do have a selected item in the TreeView and this changes with a mouse left click. It's not a left click event, though, rather the event is 'SelectedItemChanged'. This leads to a method whereby I set the 'SelectedItem' in my data context (view model) to the SelectedItem. It has to be done this way because a TreeView's selected item is 'read only'.

该代码在这里,虽然我不确定它对手头的问题有多大用处:

That code is here, although I'm not sure how useful it is to the issue at hand:

private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (this.ScenesTreeView01 == null)
            return;

        if (this.ScenesTreeView01.DataContext == null)
            return;

        var DataContext = this.ScenesTreeView01.DataContext as ScenesViewModel;

        if (e.NewValue is SceneViewModel)
        {
            DataContext.SelectedScene = (SceneViewModel)e.NewValue;
        }

        if (e.NewValue is CharacterViewModel)
        {

            DataContext.SelectedCharacter = (CharacterViewModel)e.NewValue;
        }
    }

因为似乎没有一个地方说好吧,你左键单击,所以这里是选定的项目",我不知道该怎么做才能告诉它在右键单击时分配选定的项目(以及左键单击).

Since there doesn't seem to be a place where it says 'okay you left clicked and so here is the selected item', I don't know what to do to tell it to assign the selected item on a right click (as well as a left click).

我该怎么做?

我正在使用 MVVM,所以当我们有一个像 SelectedItemChanged 这样的方法,参数是 RoutedPropertyChangedEventArgs e,e.Source 将我引回到我的视图模型,而不是 TreeViewItem.

I am using MVVM so when we have a method like SelectedItemChanged with a parameter which is RoutedPropertyChangedEventArgs e, e.Source refers me back to my view model, not to a TreeViewItem.

推荐答案

您可以将 IsSelected 属性添加到您的 SceneViewModelCharacterViewModel 类并使用样式将 TreeViewItemIsSelected 绑定到这些属性.以相同的样式,您可以为 PreviewMouseRightButtonDown 连接一个事件处理程序来设置源属性:

You could add an IsSelected property to your SceneViewModel and CharacterViewModel classes and bind the IsSelected of the TreeViewItem to these properties using a style. In the same style you could then hook up an event handler for the PreviewMouseRightButtonDown to set the source property:

<TreeView x:Name="treeView">
    <TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <EventSetter Event="PreviewMouseRightButtonDown" Handler="treeView_PreviewMouseRightButtonDown" />
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem>1</MenuItem>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
</TreeView>

<小时>

private void treeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    TreeViewItem tvi = sender as TreeViewItem;
    CharacterViewModel cvm = tvi.DataContext as CharacterViewModel;
    if (cvm != null)
    {
        cvm.IsSelected = true;
    }
    else
    {
        SceneViewModel svm = tvi.DataContext as SceneViewModel;
        if (svm != null)
            svm.IsSelected = true;
    }
}

确保 CharacterViewModelSceneViewModel 类实现了 INotifyPropertyChanged 接口并在新 IsSelected 属性的 setter.

Make sure that the CharacterViewModel and SceneViewModel classes implement the INotifyPropertyChanged interface and raise the PropertyChanged event in the setter of the new IsSelected property.

这篇关于调用 WPF 选择作为上下文菜单的 TreeView 项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆