VIBlend
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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    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>
            <viblendTreeView:TreeViewItem DisplayValue="Tree Item 2" />
        </viblendTreeView:TreeView>
        <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" />
         </viblendMenu:ContextMenu>
    </Grid>
</UserControl>

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

C#

this.MouseRightButtonDown += new MouseButtonEventHandler(MainPage_MouseRightButtonDown); 

VB .NET

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.

C#

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);
           this.contextMenu1.Open(contextMenuLocation);
           e.Handled = true;
       }
   }
}

VB .NET

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)
          Me.contextMenu1.Open(contextMenuLocation)
          e.Handled = True
       End If
   End If
End Sub

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

C#

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

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

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

VB .NET

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")
        Me.treeView1.SelectedItem.Items.Add(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
            Me.treeView1.SelectedItem.ParentTreeViewItem.Items.Remove(Me.treeView1.SelectedItem)
        ElseIf Me.treeView1.Items.Contains(Me.treeView1.SelectedItem) Then
            Me.treeView1.Items.Remove(Me.treeView1.SelectedItem)
        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.

[XAML]

         <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:MenuItem>
            </viblend:Menu>

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  
ALL RIGHTS RESERVED  
 
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