Silverlight 4 textbox right click context menu with cut, copy and paste behavior

by dsoltesz 27. May 2010 21:46 >

I wanted to be able to allow my users the ability to right click in a textbox and have a contextmenu shown to be able to cut, copy and paste. I decided to create a behavior so that the functionality can easily be added to any textbox control.

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Interactivity;


namespace MyApp.Behaviors.TextBox


    public class TextBoxCutCopyPasteBehavior : Behavior<System.Windows.Controls.TextBox>


        private ContextMenu _contextMenu;

        private MenuItem _copy;

        private MenuItem _cut;

        private MenuItem _paste;

        public TextBoxCutCopyPasteBehavior()



            _contextMenu = new ContextMenu();

            _cut = new MenuItem();

            _cut.Header = "Cut";

            _cut.Click += new RoutedEventHandler(cut_Click);



            _copy = new MenuItem();

            _copy.Header = "Copy";

            _copy.Click += new RoutedEventHandler(copy_Click);



            _paste = new MenuItem();

            _paste.Header = "Paste";

            _paste.Click += new RoutedEventHandler(paste_Click);






        void paste_Click(object sender, RoutedEventArgs e)


            AssociatedObject.SelectedText = Clipboard.GetText();

            _contextMenu.IsOpen = false;



        void cut_Click(object sender, RoutedEventArgs e)



            AssociatedObject.SelectedText = string.Empty;


            _contextMenu.IsOpen = false;



        void copy_Click(object sender, RoutedEventArgs e)




            _contextMenu.IsOpen = false;



        protected override void OnAttached()



            AssociatedObject.MouseRightButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseRightButtonDown);

            AssociatedObject.MouseRightButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);

            AssociatedObject.SetValue(ContextMenuService.ContextMenuProperty, _contextMenu);




        void AssociatedObject_MouseRightButtonUp(object sender, MouseButtonEventArgs e)


            if (Clipboard.ContainsText())

                _paste.IsEnabled = true;


                _paste.IsEnabled = false;


            if (string.IsNullOrEmpty(AssociatedObject.SelectedText))


                _cut.IsEnabled = false;

                _copy.IsEnabled = false;




                _cut.IsEnabled = true;

                _copy.IsEnabled = true;


            _contextMenu.IsOpen = true;




        void AssociatedObject_MouseRightButtonDown(object sender, MouseButtonEventArgs e)


            e.Handled = true;



        protected override void OnDetaching()


            AssociatedObject.MouseRightButtonDown -= new MouseButtonEventHandler(AssociatedObject_MouseRightButtonDown);

            AssociatedObject.MouseRightButtonUp -= new MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);

            _contextMenu = null;









Once the behavior is created we can now add to our TextBox

  1. add a reference to System.Windows.Interactivity
  2. In your xaml code add
    1. xmlns

    2. xmlns

    3.  <TextBox x:Name="myTextBox" >

That's it, now you have right click support for your textbox with cut, copy and paste.

Tags: ,

behaviors | silverlight

What's new in silverlight 4

by dsoltesz 13. February 2010 00:04 >

Below is a feature least of what is coming out in Silverlight 4.  Source: Microsoft via - Feb 2010


  • Webcam and microphone support
  • Multicast streaming
  • Output protection and offline digitial rights management support
  • Updates to IIS Media Services and IIS Smooth Streaming, which will enable the 2010 Winter Games on
  • Back-end support for IIS Media Services on the iPhone

Business Applications

  • Printing support
  • Rich-text editor (supports multiple langues, left-to-right text and copy/paste
  • Right-click and mouse wheel support
  • Implicit styling
  • Drap/drop support
  • Bidi and right-to-left for global applications
  • HTML hosting (can host plug-in controls like Flash video)
  • Commanding and Model-View-View-Model (MVVM) controls

Data and Networking Functionality

  • Databinding improvements
  • UPD multicast support
  • REST enhancements through ADO.NET services
  • Updates to Windows Communication Foundation RIA Servides (formerly .NET RIA Services)

Beyond the Browser

  • New sandboxed features (windowing APIs, notification pop-ups)
  • Introduces "trusted applications"
  • Custom window chrome
  • Utilizes local file system
  • Uses cross-site networking
  • Enables a full-screen keyboard
  • Provides hardware device access
  • Provides access to Windows 7 APIs





about the author

My name is Dan Soltesz, and I"m a consultant specializing in Microsoft technologies

  • Silverlight 2.0,3.0,4.0,5.0
  • Windows Phone 7,8
  • Windows 8
  • WPF
  • MVC / SPA
  • ASP.NET 2.0,3.0,3.5,4.0
  • WCF, WCF Ria Services
  • Entity Framework / EF Code First
  • C#
  • MVVM, Repository Pattern

to name a few.  On this site you will find links to sites/blogs that I read and my own attempt at writing some blogs that may help others as well as a few miscellaneous items.  If there is something that you need to know about me and cannot find on this site, please feel free to contact me.

page list


month list


Comment RSS