1

I have an user control (ex: UserCtrlClass)with a tree view inside it

I have View model (ex: OBJViewModel) class for represent the actual items/data display on the tree view

Next I have a Tree View Model (ex: TreeViewModel), which has a list of OBJViewModel objects

Now in the code behind file of the user control, I have instantiated the tree view model class and set as the data context of the user control class

I need a context sensitive menu, which i need to display only when I right click on a specific item in the tree, so I have handled the right click event of the user control class and did the work there

But the commands are not working, The commands are derived from I command and instantiated in TreeViewModel class. i tried to debug my Command.execute was never hit! Any help would be appreciated as I am being a newbie to .net and wpf

TreeViewModel class

<UserControl Name="PFDBUserCtrl" x:Class="BFSimMaster.BFSMTreeview"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:BFSimMaster.ViewModel"
         xmlns:cmd="clr-namespace:BFSimMaster.Commands"
         mc:Ignorable="d" 
         d:DesignHeight="66" d:DesignWidth="300">
<UserControl.Resources>
    <!--cmd:ActivateProjectCmd x:Key="CMDActivateProject"/-->
    <!--cmd:DeActivateProjectCmd x:Key="CMDDeActivateProject"/-->
</UserControl.Resources>
<DockPanel>
    <!-- PF Object Browser TREE -->
    <TreeView Name="PFDataBrowser" ItemsSource="{Binding LevelOnePFObjects}" >  
        <TreeView.Resources>
            <ContextMenu x:Key ="ProjectMenu"  StaysOpen="true" >
                <!-- Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}-->
                <!--MenuItem Header="Activate" Command="{Binding Source={StaticResource CMDActivateProject}}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/-->
                <MenuItem Header="Activate" Command="{Binding DataContext.CMDActivateProject, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                <MenuItem Header="Deactivate" Command="{Binding Source=TVViewModel, Path=CMDDeActivateProject}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
            </ContextMenu>
        </TreeView.Resources>
        <TreeView.ItemContainerStyle>
            <!-- This Style binds a TreeViewItem to a PFObject View Model.-->                
            <Style TargetType="{x:Type TreeViewItem}">                    
                <EventSetter Event="MouseRightButtonDown" Handler="OnRightButtonDown"/>
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />

                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>                        
                </Style.Triggers>

            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</DockPanel>

Code behind class

using System;
namespace BFSimMaster
{

public partial class BFSMTreeview : UserControl
{

    readonly TreeViewItemViewModel mViewModelPFObjBrowserTree;  
    public BFSMTreeview()
    {
        InitializeComponent();

        WApplication appPF = PFAPIUtils.APIInstance.GetApplication();
        WDataObject User = appPF.GetCurrentUser();



        // Get raw objects - tree data from a PF database.
        //BFPFDataObject userdb = new BFPFDataObject(User,false,"*.IntPrj");
        BFPFDataObject userdb = new BFPFDataObject(User, true);

        // Create UI-friendly wrappers around the 
        // raw data objects (i.e. the view-model).
        mViewModelPFObjBrowserTree = new TreeViewItemViewModel(userdb);

        // Let the UI bind to the view-model.
        base.DataContext = mViewModelPFObjBrowserTree;

    }
    public TreeViewItemViewModel TVViewModel
    {
        get { return mViewModelPFObjBrowserTree; }
    }

    private void OnRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        //MessageBox.Show("Right Clicked on tree view");
        if (sender is TreeViewItem)
        {
            e.Handled = true;
            (sender as TreeViewItem).IsSelected = true;

            string strObjectType = ((sender as TreeViewItem).Header as PFObjectViewModel).PFDataObject.mThisPFObject.GetClassName().GetString();
            switch (strObjectType)
            {
                case "IntPrj":
                    (sender as TreeViewItem).ContextMenu = PFDataBrowser.Resources["ProjectMenu"] as System.Windows.Controls.ContextMenu;
                    (sender as TreeViewItem).ContextMenu.PlacementTarget = (sender as TreeViewItem); 
                    break;
                case "Folder":
                    (sender as TreeViewItem).ContextMenu = PFDataBrowser.Resources["ProjectMenu"] as System.Windows.Controls.ContextMenu;
                    break;
            }

        }
    }
}

}

the TreeViewModel Class

using System;
namespace BFSimMaster.ViewModel
{

public class TreeViewItemViewModel 
{
    #region Data

    readonly ReadOnlyCollection<PFObjectViewModel> mLevelOnePFObjects;
    readonly PFObjectViewModel mRootOfPFObjects;

    #endregion // Data

    #region Constructor


    public TreeViewItemViewModel(BFPFDataObject rootOfPFObjectsA)
    {
        this.CMDActivateProject = new ActivateProjectCmd();
        this.CMDDeActivateProject = new DeActivateProjectCmd();
        mRootOfPFObjects = new PFObjectViewModel(rootOfPFObjectsA);

        mLevelOnePFObjects = new ReadOnlyCollection<PFObjectViewModel>(
            new PFObjectViewModel[] 
            { 
                mRootOfPFObjects 
            });            
    }

    #endregion // Constructor
    public ICommand CMDActivateProject { get; set; }
    public ICommand CMDDeActivateProject { get; set; }

    public ReadOnlyCollection<PFObjectViewModel> LevelOnePFObjects
    {
        get { return mLevelOnePFObjects; }
    }       

}
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
RADKrish
  • 51
  • 7

3 Answers3

0

A ContextMenu isn't part of the logical tree, so this binding "{Binding DataContext.CMDActivateProject, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" wont work, because it simply has no ancestor of type UserControl. If you dont set the DataContext of the PlacementTarget manuelly, you could try

"{Binding PlacementTarget.DataContext.CMDActivateProject, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"

I not 100% sure about the following, but I think this will just work, if youre using the ContextMenu property:

<Treeview>
    <Treeview.ContextMenu>
        <ContextMenu>
            ...
        <ContextMenu>
    <Treeview.ContextMenu>
    ...

The adventage here is, that you dont have to handle the rightbuttondown event in code-behind, it automatically opens if you right click on your treeview.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
0

I've solved this problem by introducing VM class for MenuItem and setting context menu with ExtendedContextMenu.Items={Binding ContextMenu} attached property. MenuResourcesDictionary is ResourceDictionary.xaml with back-side .cs file (shown below).

To use it for your code you need to add IEnumerable<MenuItemVM> ContextMenu property on your tree model and put commands there (e.g. pass them to MenuItemVM constructor). And in the item style add <Setter Property="ExtendedContextMenu.Items" Value="{Binding DataContext.ContextMenu}" />

public static class ExtendedContextMenu
{
    private static readonly StyleSelector _styleSelector = new ContextMenuItemStyleSelector();

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.RegisterAttached("Items",
                                            typeof(IEnumerable<MenuItemVM>),
                                            typeof(ExtendedContextMenu),
                                            new FrameworkPropertyMetadata(default(IEnumerable<MenuItemVM>), ItemsChanged));

    public static void SetItems(DependencyObject element, IEnumerable<MenuItemVM> value)
    {
        element.SetValue(ItemsProperty, value);
    }

    public static IEnumerable<MenuItemVM> GetItems(DependencyObject element)
    {
        return (IEnumerable<MenuItemVM>)element.GetValue(ItemsProperty);
    }

    private static void ItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var target = (FrameworkElement)d;

        var items = (IEnumerable<MenuItemVM>)e.NewValue;
        var contextMenu = new ContextMenu();

        contextMenu.ItemContainerStyleSelector = _styleSelector;
        contextMenu.ItemsSource = items;

        return contextMenu;
    }

    private static void AdjustContextMenuVisibility(ContextMenu menu)
    {
        menu.Visibility = menu.HasItems ? Visibility.Visible : Visibility.Hidden;
    }
}

public class ContextMenuItemStyleSelector : StyleSelector
{
    private static readonly MenuResourcesDictionary _resources = new MenuResourcesDictionary();
    private static readonly Style _menuItemStyle = (Style)_resources[MenuResourcesDictionary.MenuItemStyleResourceKey];
    private static readonly Style _separatorStyle = (Style)_resources[MenuResourcesDictionary.SeparatorStyleResourceKey];

    public override Style SelectStyle(object item, DependencyObject container)
    {
        if (item == MenuItemVM.Separator)
            return _separatorStyle;

        return _menuItemStyle;
    }
}

public sealed partial class MenuResourcesDictionary
{
    public const string MenuItemStyleResourceKey = "MenuItemStyleResourceKey";
    public const string DynamicMenuItemStyleResourceKey = "DynamicMenuItemStyleResourceKey";
    public const string SeparatorStyleResourceKey = "SeparatorStyleResourceKey";
    public const string LoadingStyleResourceKey = "LoadingMenuItemStyleResourceKey";

    public MenuResourcesDictionary()
    {
        InitializeComponent();
    }
}

Here is XAML styles:

<Style x:Key="{x:Static local:MenuResourcesDictionary.MenuItemStyleResourceKey}">
  <Setter Property="MenuItem.Header" Value="{Binding Path=(menuVMs:MenuItemVM.Text)}" />
  <Setter Property="MenuItem.Command" Value="{Binding Path=(menuVMs:MenuItemVM.Command)}" />
</Style>

<Style x:Key="{x:Static local:MenuResourcesDictionary.SeparatorStyleResourceKey}" TargetType="{x:Type MenuItem}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <Separator />
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

MenuItemVM class is more-or-less straighforward, so I don't write it here.

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
0

I Found the Answer for TreeView Context menu with and with out DataTemplate here: TreeView ContextMenu MVVM Binding MVVM binding command to contextmenu item

Community
  • 1
  • 1
Kamlendra Sharma
  • 687
  • 2
  • 10
  • 37