views:

381

answers:

1

I have three tree view controls which house different (but mostly similar data), as a result the actions that can be taken at each level is the same as far as the user is concerned, but different in their type (which is something I have to worry about as the developer). What I would like to do is reuse this context menu and pass in a type to it (or be able to retrieve it) and then the type would get passed through to the actual Executed function.

So that we can have a common verbage here's some code:

<UserControl x:Class="ucControl" 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="clr-namespace:LocalNamespace"
 Name="ucControl" 
 xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
 Background="LightGray">
 <UserControl.Resources>
     <ContextMenu x:Key="GroupMenu">
         <MenuItem Header="Add Group" Command="New"/>
         <MenuItem Header="Rename Group" Command="local:CustomCommands.RenameGroup"/>
         <MenuItem Header="Delete Group" Command="Delete"/>
         <Separator/>
         <MenuItem Header="Change Contents of Group" Command="local:CustomCommands.EditGroupContents"/>
     </ContextMenu>
 </UserControl.Resources> 
 <UserControl.CommandBindings>
     <CommandBinding CanExecute="CanAddGroup" Command="New" Executed="AddGroup"/>
     <CommandBinding Command="local:CustomCommands.RenameGroup" CanExecute="CanRename" Executed="RenameGroup"/>
     <CommandBinding Command="local:CustomCommands.EditGroupContents" CanExecute="CanEditGroupContents" Executed="EditGroupContents"/>
 </UserControl.CommandBindings>
 <TabControl Name="tcTabs">
     <TabItem Header="Size" Name="tiSize">
         <TreeView Name="tvSizeGroup" ContextMenu="{StaticResource GroupMenu}"/>
     </TabItem>
     <TabItem Header="Brand" Name="tiBrand">
         <TreeView Name="tvBrandGroup" ContextMenu="{StaticResource GroupMenu}"/>
     </TabItem>
     <TabItem Header="Color" Name="tiColor">
         <TreeView Name="tvColorGroup" ContextMenu="{StaticResource GroupMenu}"/>
     </TabItem>
 </TabControl>

In this example, I would like for each of Size, Brand, and Color to have the same ContextMenu, but if the Context Menu is pulled up in the Size TreeView, I will need to add a group of type Size to my table. Is this possible the way I am doing it? I am trying to avoid making three ContextMenus, which would obviously be a way to do this.

+2  A: 

There is only one way i know to achieve this, In the CommandHandler you need to check that which Tree has called this.

try this in your CommandHandler:

MenuItem mnu = sender as MenuItem;
TreeView tv = null;
if(mnu!=null)
{
    tv = ((ContextMenu)mnu.Parent).PlacementTarget as TreeView;
}
if(tv.Name=="tvSizeGroup")
{
    //add group of Size
}
if(tv.Name=="tvBrandGroup")
{
    //add group of Brand
}

Hope this helps!!

viky
That should work, I was going to have to do get an id from the element anyway in some of the methods.
Anthony Potts