从菜单上下文获取Treeviewitem。 WPF XAML MVVM

时间:2017-04-03 18:09:33

标签: c# wpf mvvm contextmenu menuitem

美好的一天。 我创建了一个TreeViewHelper类,以便触发事件并将datacontext传递给函数。然而。我想在右键单击时选择treeviewitem。

我的解决方案尽可能地遵循MVVM模式。因此,如果可能,我想避免提供的触发事件功能。

我喜欢关于如何解决这个问题的一些建议。

这是treeviewhelper.cs类

public static class TreeViewHelper
{
    #region SelectedItem

    public static object GetSelectedItem(DependencyObject obj)
    {
        return (object)obj.GetValue(SelectedItemProperty);
    }

    public static void SetSelectedItem(DependencyObject obj, object value)
    {
        obj.SetValue(SelectedItemProperty, value);
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(TreeViewHelper), new UIPropertyMetadata(null, SelectedItemChanged));

    private static void SelectedItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs dpea)
    {
        if (!(obj is TreeView) || dpea.NewValue == null)
            return;

        var view = obj as TreeView;

        view.SelectedItemChanged += (sender, e) => SetSelectedItem(view, e.NewValue);

        var command = (ICommand)(view as DependencyObject).GetValue(SelectedItemChangedProperty);
        if (command != null)
        {
            if (command.CanExecute(null))
                command.Execute(new DependencyPropertyEventArgs(dpea));
        }
    }

    #endregion

    #region Selected Item Changed

    public static ICommand GetSelectedItemChanged(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(SelectedItemProperty);
    }

    public static void SetSelectedItemChanged(DependencyObject obj, ICommand value)
    {
        obj.SetValue(SelectedItemProperty, value);
    }

    public static readonly DependencyProperty SelectedItemChangedProperty =
        DependencyProperty.RegisterAttached("SelectedItemChanged", typeof(ICommand), typeof(TreeViewHelper));

    #endregion

    #region Event Args

    public class DependencyPropertyEventArgs : EventArgs
    {
        public DependencyPropertyChangedEventArgs DependencyPropertyChangedEventArgs { get; private set; }

        public DependencyPropertyEventArgs(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            this.DependencyPropertyChangedEventArgs = dependencyPropertyChangedEventArgs;
        }
    }

    #endregion

}

这是XAML代码(缩写)

<TreeView     x:Name="TestPlanTreeView"
              ItemsSource="{Binding Data.TreeViewCollection}"                  
              utils:TreeViewHelper.SelectedItem="{Binding CurrSelItem}"
              utils:TreeViewHelper.SelectedItemChanged="{Binding SelItemChgCmd}">
        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu ItemsSource="{Binding MenuCollection, RelativeSource={RelativeSource Self}}">
                            <ContextMenu.Resources>
                                <Style TargetType="MenuItem">
                                    <Setter Property="Command" Value="{Binding Command}" />
                                </Style>
                            </ContextMenu.Resources>
                            <ContextMenu.ItemTemplate>
                                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                    <TextBlock Text="{Binding Header}"/>
                                </HierarchicalDataTemplate>
                            </ContextMenu.ItemTemplate>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
                <TextBlock Text="{Binding Header}"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

这是我在选择新项目时触发功能的方法。

SelItemChgCmd = new RelayCommand<DependencyPropertyEventArgs>(Data.TreeViewItemSelected);
CurrSelItem = new object();

public void TreeViewItemSelected(DependencyPropertyEventArgs dpe)
{
    TreeViewItem selectedTreeViewItem = dpe.DependencyPropertyChangedEventArgs.NewValue as TreeViewItem;
    ListViewCollection = BuildListViewCollection(selectedTreeViewItem);
}      

我喜欢做的是能够在右键单击对象之前选择treeviewitem。但是,我不知道如何使用我给定的代码而不用黑客攻击它。

我最初的想法是在treeviewhelper中为PreviewRightMouseButtonUp创建一个属性,并触发SelectedItem事件,但后来我遇到了数据问题。

由于PreviewRightMouseButtonUp不包含与selectedItem Changed事件相同的方法类型,因此我遇到了这些数据问题。

我非常感谢对此事的一些看法。它现在已经阻止了我一段时间。

0 个答案:

没有答案