Home Products Downloads Purchase Support Forum About Blog

Using a Context Menu with a TreeView

by viblend 14. November 2010 01:44

This blog post demonstrates how to capture the right click event on a tree view in order to display a context menu.

At first, we need to drag and drop a new instances of the TreeView and ContextMenu controls from the Visual Studio 2010 Toolbox.


The xaml markup is shown below:

<UserControl x:Class="TreeViewContextMenu.MainPage"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:viblendTreeView="clr-namespace:VIBlend.Silverlight.Controls;assembly=VIBlend.Silverlight.TreeView" xmlns:viblendMenu="clr-namespace:VIBlend.Silverlight.Controls;assembly=VIBlend.Silverlight.Menu">

    <Grid x:Name="LayoutRoot" Background="White">
        <viblendTreeView:TreeView Height="250" HorizontalAlignment="Center"  Name="treeView1" VerticalAlignment="Center" Width="200">
            <viblendTreeView:TreeViewItem DisplayValue="Tree Item 1" IsExpanded="True">
                <viblendTreeView:TreeViewItem DisplayValue="Tree Sub Item 1" />
                <viblendTreeView:TreeViewItem DisplayValue="Tree Sub Item 2" />
                <viblendTreeView:TreeViewItem DisplayValue="Tree Sub Item 3" />
            <viblendTreeView:TreeViewItem DisplayValue="Tree Item 2" />
        <viblendMenu:ContextMenu HorizontalAlignment="Left"  Name="contextMenu1" VerticalAlignment="Top">   
            <viblendMenu:MenuItem Click="AddItem_Click" Text="Add" />
            <viblendMenu:MenuItem Click="RemoveItem_Click" Text="Remove" />
            <viblendMenu:MenuItem Click="DisplayItem_Click" Text="Disable" />

Then we need to subscribe to the MouseRightButtonDown event and we can do this either in the xaml markup or in code-behind:


this.MouseRightButtonDown += new MouseButtonEventHandler(MainPage_MouseRightButtonDown); 


AddHandler MouseRightButtonDown, AddressOf MainPage_MouseRightButtonDown

After we subscribe to the MouseRightButtonDown event, we should implement its event handler. In the event handler, we display the ContextMenu control on the position where the right mouse button is clicked.


void MainPage_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
   Point point = e.GetPosition(this.treeView1);
   if (point.X >= 0 && point.X <= this.treeView1.Width)
       if (point.Y >= 0 && point.Y <= this.treeView1.Height)
           Point contextMenuLocation = e.GetPosition(this);
           e.Handled = true;


Private Sub MainPage_MouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
   Dim point As Point = e.GetPosition(Me.treeView1)
   If point.X >= 0 AndAlso point.X <= Me.treeView1.Width Then
      If point.Y >= 0 AndAlso point.Y <= Me.treeView1.Height Then
          Dim contextMenuLocation As Point = e.GetPosition(Me)
          e.Handled = True
       End If
   End If
End Sub

Finally, we implement the event handlers that will add, remove and disable TreeViewItem objects.


private void AddItem_Click(object sender, EventArgs e)
    if (this.treeView1.SelectedItem != null)
        TreeViewItem item = new TreeViewItem("New Item");

private void RemoveItem_Click(object sender, EventArgs e)
    if (this.treeView1.SelectedItem != null)
        if (this.treeView1.SelectedItem.ParentTreeViewItem != null)
        else if (this.treeView1.Items.Contains(this.treeView1.SelectedItem))

private void DisableItem_Click(object sender, EventArgs e)
    if (this.treeView1.SelectedItem != null)
        this.treeView1.SelectedItem.IsEnabled = false;


Private Sub AddItem_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Me.treeView1.SelectedItem IsNot Nothing Then
        Dim item As New TreeViewItem("New Item")
    End If
End Sub

Private Sub RemoveItem_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Me.treeView1.SelectedItem IsNot Nothing Then
        If Me.treeView1.SelectedItem.ParentTreeViewItem IsNot Nothing Then
        ElseIf Me.treeView1.Items.Contains(Me.treeView1.SelectedItem) Then
        End If
    End If
End Sub

Private Sub DisableItem_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Me.treeView1.SelectedItem IsNot Nothing Then
        Me.treeView1.SelectedItem.IsEnabled = False
    End If
End Sub

Here is the result:

VIBlend Silverlight Controls v3.0 - Released

by viblend 14. September 2010 20:47

VIBlend is announcing the immediate availability of the VIBlend Controls for Silverlight ver. 3.0.

With 19 new controls, Themes and enhanced functionality in the DataGrid, Menu and SpinEditor, VIBlend Controls for Silverlight empower developers to build full featured and great looking line-of-business applications.

New features:

  • Menu and ContextMenu controls now have 17 built-in Themes including Office 2010 Blue, Black and Silver and Office 2007 Blue, Black and Silver
  • DataGrid Enhancements  - The latest release introduces Export to Excel, CSV, HTML and XML.
  • 19 new editors such as ComboBox, ListBox, CheckedListBox, GroupBox, ColorPicker, RadioButton, CheckBox, Slider, Button, ToggleButton, etc.
  • Data Input Editors and NavigationPane now have an improved Data Binding capabilities.
See our Silverlight Controls Live Demo and Download a free trial today.

How to create a scrollable Menu in Silverlight

by viblend 8. May 2010 18:30

In case the available height is too small to accommodate a group of menu items, the group becomes scrollable.  Each menu item has a DropDownHeight and VerticalScrollBarVisibility properties. The DropDownHeight property is used to restrict the displayed height of a group of menu items, while the VerticalScrollBarVisibility property defines whether the item allows vertical scrolling or not.

The following code example demonstrates how to create a scrollable menu.


         <viblend:Menu Name="Menu" Width="400" Height="20">
                <viblend:MenuItem RootNormalForeBrush="Black" DropDownHeight="150" VerticalScrollBarVisibility="Visible" Text="File">
                    <viblend:MenuItem Text="Mail Message" ImageSource="images/mail.png"/>
                    <viblend:MenuItem Text="Note" ImageSource="images/Note.png"/>
                    <viblend:MenuItem Text="Contact" ImageSource="images/contact.png" />
                    <viblend:MenuItem Text="Appointment" ImageSource="images/Appointment.png"/>
                    <viblend:MenuItem Text="Task" ImageSource="images/Task.png" />
                    <viblend:MenuItem Text="Folder" ImageSource="images/folder.png"/>

VIBlend Menu for Silverlight is a free component that is part of the VIBlend Controls for Silverlight package.

VIBlend Silverlight Controls - ver. 2.0 Released

by viblend 3. March 2010 05:26

We are proud to announce the second major release of VIBlend Controls for Silverlight.

With the new release, VIBlend has officially included in the suite four new controls – OutlookPane, NavigationPane, DateTimePicker and ScrollablePanel. Not only we introduced new controls, but we also improved the quality and functionality of the existing ones and added over 10 new examples to show how our controls work.

The Menu and Context Menu controls from the toolset are now provided free of charge to all registered users.

See our Silverlight Controls Live Demo and Download a free trial today.

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET