.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Right Click in Silverlight (Implementing Copy, Cut and Paste)

Posted By:Subhransu Sekhar Jena       Posted Date: November 18, 2014    Points: 40    Category: Collections    URL: http://www.dotnetspark.com  

Enable Right Click in Silverlight.
 

If you want to enable right click Or implement Cut, Copy and Paste in silverlight then follow these steps:

1) Add a class (e.g; ControlCustomBehaviors.vb) and write the below code

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


Namespace SampleApplicationNameSpace

    Public Class ControlCustomBehaviors
        Inherits Behavior(Of Control)

#Region "Set Context Menu"
        Public Shared SetMenuProperty As DependencyProperty = DependencyProperty.RegisterAttached("SetMenu", GetType(Object), GetType(ControlCustomBehaviors), New PropertyMetadata(Nothing, AddressOf SetMenuProp))

        Private Shared tBox As TextBox
        Private Shared Sub SetMenuProp(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            tBox = TryCast(d, TextBox)
            CreateMenu()
            AddHandler tBox.MouseRightButtonDown, New MouseButtonEventHandler(AddressOf AssociatedObject_MouseRightButtonDown)
            AddHandler tBox.MouseRightButtonUp, New MouseButtonEventHandler(AddressOf AssociatedObject_MouseRightButtonUp)
            tBox.SetValue(ContextMenuService.ContextMenuProperty, _contextMenu)
        End Sub

        Public Shared Function GetSetMenu(obj As UIElement) As Object
            Return DirectCast(obj.GetValue(SetMenuProperty), Object)
        End Function
        Public Shared Sub SetSetMenu(obj As DependencyObject, txtbox As Object)
            obj.SetValue(SetMenuProperty, txtbox)
        End Sub

        Private Shared _contextMenu As ContextMenu
        Private Shared _copy As MenuItem
        Private Shared _cut As MenuItem
        Private Shared _paste As MenuItem

        Private Shared Sub CreateMenu()
            _contextMenu = New ContextMenu()
            _cut = New MenuItem()
            _cut.Header = "Cut"
            AddHandler _cut.Click, New RoutedEventHandler(AddressOf cut_Click)
            _contextMenu.Items.Add(_cut)

            _copy = New MenuItem()
            _copy.Header = "Copy"
            AddHandler _copy.Click, New RoutedEventHandler(AddressOf copy_Click)
            _contextMenu.Items.Add(_copy)

            _paste = New MenuItem()
            _paste.Header = "Paste"
            AddHandler _paste.Click, New RoutedEventHandler(AddressOf paste_Click)
            _contextMenu.Items.Add(_paste)
        End Sub
        Private Shared Sub paste_Click(sen As Object, e As RoutedEventArgs)
            tBox.SelectedText = Clipboard.GetText()
            _contextMenu.IsOpen = False
            tBox.Focus()
        End Sub
        Private Shared Sub cut_Click(sen As Object, e As RoutedEventArgs)
            Clipboard.SetText(tBox.SelectedText)
            tBox.SelectedText = String.Empty
            _contextMenu.IsOpen = False
            tBox.Focus()
        End Sub
        Private Shared Sub copy_Click(sen As Object, e As RoutedEventArgs)
            Clipboard.SetText(tBox.SelectedText)
            _contextMenu.IsOpen = False
            tBox.Focus()
        End Sub

        Private Shared Sub AssociatedObject_MouseRightButtonUp(Sender As Object, e As MouseButtonEventArgs)
            If Clipboard.ContainsText() AndAlso tBox.IsReadOnly = False AndAlso tBox.IsEnabled = True Then
                _paste.IsEnabled = True
            Else
                _paste.IsEnabled = False
            End If
            If String.IsNullOrEmpty(tBox.SelectedText) Then
                _cut.IsEnabled = False
                _copy.IsEnabled = False
            Else
                _cut.IsEnabled = True
                _copy.IsEnabled = True
            End If
            _contextMenu.IsOpen = True
            tBox.Focus()
        End Sub
        Private Shared Sub AssociatedObject_MouseRightButtonDown(sender As Object, e As MouseButtonEventArgs)
            tBox = TryCast(sender, TextBox)
            e.Handled = True

        End Sub
#End Region

    End Class
End Namespace

2) After that add a reference to System.Windows.Interactivity.


3) In App.xaml Write the below code

xmlns:My="clr-namespace:SampleApplication.SampleApplicationNameSpace"  


    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
                <ResourceDictionary>
                    <app:ResourceWrapper x:Key="ResourceWrapper" />
                    <app:NotOperatorValueConverter x:Key="NotOperatorValueConverter" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
        <Style TargetType="TextBox">
            <Setter Property="My:ControlCustomBehaviors.SetMenu" Value="true" />
        </Style>
    </Application.Resources>
Note: 

Sample Application Will be your Application name.


The above code will enables copy, cut and paste in your application.





     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend