Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

DependencyProperty RegisterAttached working on keypress but I need it on context menu click

$
0
0

Working on Data grid Copy, Cut, Paste and Completed with Dependency Property Register Attached.

Problem is on  Key press  its working Fine, Now I need same behavior in Context Menu Also..

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using Clipboard = System.Windows.Clipboard;
using DataGrid = System.Windows.Controls.DataGrid;
using MessageBox = System.Windows.MessageBox;
using TextDataFormat = System.Windows.TextDataFormat;

namespace GUI.Model
{
  
    public  class Cutcopypaste 
    {
        
        public static DependencyProperty CopyPasteProperty = DependencyProperty.RegisterAttached("CopyPaste",
            typeof(bool), typeof(Cutcopypaste),
            new FrameworkPropertyMetadata(false, Onpropertychanged));


        private static readonly CommandBinding BindingPaste =
            new CommandBinding(ApplicationCommands.Paste, CommandBinding_CanExecutePaste);


        private static readonly CommandBinding BindingCut =
            new CommandBinding(ApplicationCommands.Cut, CommandBinding_CanExecuteCut);


        private static readonly CommandBinding BindingDelete =
            new CommandBinding(ApplicationCommands.Delete, CommandBinding_CanExecuteDelete);

        private static void CommandBinding_CanExecuteCut(object sender, ExecutedRoutedEventArgs e)
        {         
        //something
        }
        

        private static async void CommandBinding_CanExecutePaste(object sender, ExecutedRoutedEventArgs e)
        {
           //something
           
        }

        private static async void CommandBinding_CanExecuteDelete(object sender, ExecutedRoutedEventArgs e)
        {
            //something
        }

     

        private static void Onpropertychanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var datagrid = (DataGrid) d;
            datagrid.CommandBindings.Add(BindingPaste);
            datagrid.CommandBindings.Add(BindingCut);
            datagrid.CommandBindings.Add(BindingDelete);
            
        }


        public static bool GetCopyPaste(DependencyObject target)
        {
            return (bool) target.GetValue(CopyPasteProperty);
        }

        public static void SetCopyPaste(DependencyObject target, bool value)
        {
            target.SetValue(CopyPasteProperty, value);
        }

        
    }
}
<DataGrid x:Name="WelcomeDatagrid1" ItemsSource="{Binding Dt}" ccp:Cutcopypaste.CopyPaste="True"  HeadersVisibility="All"<DataGrid.ContextMenu><ContextMenu><MenuItem Header="Cut" InputGestureText="Ctrl+X"  Command="ApplicationCommands.Copy" ></MenuItem><MenuItem Header="Copy" InputGestureText="Ctrl+C" Command="ApplicationCommands.Copy" CommandParameter="Copy"></MenuItem><MenuItem Header="Paste" InputGestureText="Ctrl+V" Command="ApplicationCommands.Paste"></MenuItem><MenuItem Header="Delete"  Command="{x:Static ccp:Cutcopypaste.DatagridContextCommand}" CommandParameter="BindingDelete"></MenuItem></ContextMenu></DataGrid.ContextMenu></DataGrid>

Problem Statement:

Context Menu should call  Copy, Cut, Paste from Dependency Property Register Attached.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>