Monday, April 16, 2012

Creating Cut, Copy, Paste Context menu for all textboxs in silverlight using Style in App.xaml

This article will provide you the help for creating Cut, Copy, Paste Context menu for all textboxs in silverlight using Style in App.xaml. This style will work for every Textbox that you will add in any form of your application..

App.xaml


xmlns:my="clr-namespace:YourApplicationNameSpace"



<Application.Resources>
  <Style TargetType="TextBox">
     <Setter Property="my:ControlCustomBehaviors.SetMenu" Value="true" />
  Style>
Application.Resources>

ControlCustomBehaviors.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;


namespace YourApplicationNameSpace
{

public class ControlCustomBehaviors : Behavior<Control>
{

#region Set Context Menu
public static DependencyProperty SetMenuProperty =
          DependencyProperty.RegisterAttached("SetMenu",
                                              typeof(object),
                                              typeof(ControlCustomBehaviors),                                            new PropertyMetadata(null, SetMenuProp));
       
private static TextBox tBox;
private static void SetMenuProp(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
 tBox = d as TextBox;
 CreateMenu();


 tBox.MouseRightButtonDown += new    MouseButtonEventHandler(AssociatedObject_MouseRightButtonDown);

 tBox.MouseRightButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);

 tBox.SetValue(ContextMenuService.ContextMenuProperty, _contextMenu);

}

public static object GetSetMenu(UIElement obj)
{
    return (object)obj.GetValue(SetMenuProperty);
}
public static void SetSetMenu(DependencyObject obj, object txtbox)
{
     obj.SetValue(SetMenuProperty, txtbox);
}

private static ContextMenu _contextMenu;
private static MenuItem _copy;
private static MenuItem _cut;
private static MenuItem _paste;

private static void CreateMenu()
{
  _contextMenu = new ContextMenu();
  _cut = new MenuItem();
  _cut.Header = "Cut";
  _cut.Click += new RoutedEventHandler(cut_Click);
  _contextMenu.Items.Add(_cut);

  _copy = new MenuItem();
  _copy.Header = "Copy";
  _copy.Click += new RoutedEventHandler(copy_Click);
  _contextMenu.Items.Add(_copy);

   _paste = new MenuItem();
   _paste.Header = "Paste";
   _paste.Click += new RoutedEventHandler(paste_Click);
   _contextMenu.Items.Add(_paste);
  }
  static void paste_Click(object sen, RoutedEventArgs e)
  {
    tBox.SelectedText = Clipboard.GetText();
    _contextMenu.IsOpen = false;
     tBox.Focus();
  }
  static void cut_Click(object sen, RoutedEventArgs e)
  {
     Clipboard.SetText(tBox.SelectedText);
     tBox.SelectedText = string.Empty;
     _contextMenu.IsOpen = false;
     tBox.Focus();
  }
  static void copy_Click(object sen, RoutedEventArgs e)
  {
      Clipboard.SetText(tBox.SelectedText);
      _contextMenu.IsOpen = false;
      tBox.Focus();
  }

  static void AssociatedObject_MouseRightButtonUp(object Sender,  MouseButtonEventArgs e)
        {
  if (Clipboard.ContainsText() && tBox.IsReadOnly == false && tBox.IsEnabled == true)
  {
     _paste.IsEnabled = true;
  }
  else
  {
   _paste.IsEnabled = false;
  }
  if (string.IsNullOrEmpty(tBox.SelectedText))
  {
    _cut.IsEnabled = false;
    _copy.IsEnabled = false;
  }
  else
  {
    _cut.IsEnabled = true;
    _copy.IsEnabled = true;
  }
  _contextMenu.IsOpen = true;
   tBox.Focus();

  }
  static void AssociatedObject_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
 {
    tBox = sender as TextBox;
    e.Handled = true;
 }
 #endregion

}
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Please let me know if you're looking for a article author for your blog. You have some really good posts and I think I would be a good asset. If you ever want to take some of the load off, I'd love to write some content for your blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Regards!


    Here is my weblog - advokat

    ReplyDelete