Click or drag to resize

ShellContextMenu Class

Component representing a the context menu for single or multiple shell items. It can be used in place of a normal ContextMenu on any element supporting ContextMenu. ShellItem or ShellItems contains the item(s) to show the context menu for. See examples for binding options.

MenuItems that are specified beneath the ShellContxtMenu will be added as custom items to the ShellContextMenu. ShowShellContextMenuOnTop controls the order of custom vs. shellitems in the menu.

Using the SystemStyle property, you can configure how the context menu is hosted. If true, the menu will be presented as Win32 context menu, the custom menu items will be transformed. If false, then menu is transformed to a WPF menu.
Inheritance Hierarchy

Namespace:  Jam.Shell.WPF.Controls
Assembly:  ShellBrowserWPF (in ShellBrowserWPF.dll) Version: 1.3.1
Syntax
public class ShellContextMenu : ContextMenu

The ShellContextMenu type exposes the following members.

Constructors
  NameDescription
Public methodShellContextMenu
Default constructor.
Top
Properties
  NameDescription
Public propertyShellItem
Gets or sets the ItemIdList for which the context menu is invoked.
Public propertyShellItems
Gets or sets a collection of ItemIdLists defining the objects the shell context menu will be displayed for.
Public propertyShowShellContextMenuOnTop
Determines the order of shell menu items and custom menu items.
Public propertySystemStyle
Gets or sets a value indicating whether to show the menu as Win32 menu or as native WPF menu.
Public propertyVersion
The current version of ShellBrowser Components WPF.
Top
Methods
  NameDescription
Protected methodOnAfterCommandExecute
Fires the AfterCommandExecute event.
Protected methodOnBeforeCommandExecute
Fires the BeforeCommandExecute event.
Protected methodOnOpened
Called when the Opened event occurs. Opens the ShellContextMenu instead.
(Overrides ContextMenuOnOpened(RoutedEventArgs).)
Protected methodOnPropertyChanged
Invoked whenever the effective value of any dependency property on this FrameworkElement has been updated. The specific dependency property that changed is reported in the arguments parameter. Overrides OnPropertyChanged(DependencyPropertyChangedEventArgs) .
(Overrides FrameworkElementOnPropertyChanged(DependencyPropertyChangedEventArgs).)
Protected methodOnRenameItemSelected
Fires the RenameItemSelected event
Top
Events
  NameDescription
Public eventAfterCommandExecute
This event is fired after a command that was selected from the context menu or passed to ExecuteCommand(ShellContextMenuCommand) is processed.
Public eventBeforeCommandExecute
This event is fired before a command that was selected from the context menu or passed to ExecuteCommand(ShellContextMenuCommand) is processed.
Public eventRenameItemSelected
This event is fired when the context menu option "Rename" is selected from the context menu or passed to ExecuteCommand(ShellContextMenuCommand).
Top
Fields
  NameDescription
Public fieldStatic memberShellItemProperty
The DependencyProperty for Location.
Public fieldStatic memberShellItemsProperty
Using a DependencyProperty as the backing store for Selection. This enables animation, styling, binding, etc...
Top
Examples
The following examples show how to realise the bindings on single path or a JamShellItemCollection.
XAML
<TextBox Name="PathEdit" Text="{Binding SelectedFile, Mode=TwoWay}">
    <TextBox.ContextMenu>
        <!-- Shows the shell context menu for file path contained in the parent TextBox -->
        <shlbr:ShellContextMenu ShellItem="{Binding PlacementTarget.Text, RelativeSource={RelativeSource Self}}"/>
    </TextBox.ContextMenu>
</TextBox>
XAML
<ListView Name="listView1" ItemsSource="{Binding CurrentDirectory}">
    <ListView.Resources>
        <DataTemplate DataType="{x:Type shellbrowser:ItemIdList}">
            <StackPanel Orientation="Horizontal">
                <shlbr:ShellThumbnail ImageSize="SmallIcons" Location="{Binding}"/>
                <TextBlock Padding="10, 0, 0, 0" Text="{Binding Path=Caption}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>
    <ListView.ContextMenu>
        <!-- Shows the shell context menu for the ItemIdLists contained in SelectedItemIdLists - a JamShellItemCollection held in the viewmodel and updated to represent the ListView's SelectedItems property. -->
        <shlbr:ShellContextMenu ShellItems="{Binding SelectedItemIdLists}"/>
    </ListView.ContextMenu>
</ListView>
The following example illustrates how to add custom items to a ShellContextMenu.
XAML
<ListView Name="listView3" ItemsSource="{Binding CurrentDirectory}">
    <ListView.Resources>
        <DataTemplate DataType="{x:Type shellbrowser:ItemIdList}">
            <StackPanel Orientation="Horizontal">
                <shlbr:ShellThumbnail ImageSize="SmallIcons" Location="{Binding}"/>
                <TextBlock Padding="10, 0, 0, 0" Text="{Binding Path=Caption}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>
    <ListView.ContextMenu>
        <!-- Shows the shell context menu for the ItemIdLists available through the SelectedItems property of the ListView. -->
        <shlbr:ShellContextMenu ShellItems="{Binding ElementName=listView2, Path=SelectedItems}">
            <!-- 2 custom menu items are added to the top, on is handled via click eventhandler, the other via Command.:-->
            <MenuItem Header="Open cmd here" 
                      Command="{Binding OpenCmdPrompt}" 
                      CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
            />
            <MenuItem Header="Sum file sizes" Click="MenuItem_Click"/>
        </shlbr:ShellContextMenu>
    </ListView.ContextMenu>
</ListView>

Using the BeforeCommandExecute and AfterCommandExecute events, you can intercept before or after shell context menu commands the user has chosen are executed, as can be seen in the following snippets.

C#
private void ShellMenu1_AfterCommandExecute(object sender, ShellCommandEventArgs e)
{
    //write a debug message for failing context menu calls.
    if (!e.CommandSucceeded)
        System.Diagnostics.Debug.WriteLine("Context menu {0} failed: {1}", e.Verb, e.Error);
}

private void ShellMenu1_BeforeCommandExecute(object sender, BeforeShellCommandEventArgs e)
{
    //do not execute the "Open" action, if it affects more than one element.
    e.Cancel = e.Verb == ShellCommand.Open && (e.Items.Count > 1);
}

See Also