Facebook
From grzesiek51114, 7 Years ago, written in C#.
This paste is a reply to Re: DataGridDP from grzesiek51114 - go back
Embed
Viewing differences between Re: DataGridDP and Re: Re: DataGridDP
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace TasksProviderV3.Helpers
{
    public static class DataGridDP
    {
        public static readonly DependencyProperty DoubleClickProperty;

        static DataGridDP()
        {
            DoubleClickProperty = DependencyProperty.RegisterAttached("DoubleClick", typeof(ICommand), typeof(DataGridDP),
                new UIPropertyMetadata(null, OnDoubleClickPropertyChanged));
        }

        public static void OnDoubleClickPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var grid = obj as DataGrid;
            if (grid != null)
            {
                var command = e.NewValue as ICommand;
                if (e.OldValue == null && e.NewValue != null) grid.MouseDoubleClick += Grid_MouseDoubleClick;
                else if (e.OldValue != null && e.NewValue == null) grid.MouseDoubleClick -= Grid_MouseDoubleClick;
            }
        }

        private static void Grid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var obj = sender as DependencyObject;
            var command = obj.GetValue(DoubleClickProperty) as ICommand;

                                    if (command != null && command.CanExecute(obj))
            {
                command.Execute(obj);
            }
        }

        public static void SetDoubleClick(DependencyObject obj, ICommand value)
        {
            obj.SetValue(DoubleClickProperty, value);
        }

        public static ICommand GetValue(DependencyObject obj)
        {
            return obj.GetValue(DoubleClickProperty) as ICommand;
        }
    }
}