using System.Collections.ObjectModel;
using System.Windows;
using Telerik.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace GridViewMenuBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class Item
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }

    public class MainViewModel
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<Item>()
            {
                new Item() { Name = "Item 1", Description = "Description 1" },
                new Item() { Name = "Item 2", Description = "Description 2" },
                new Item() { Name = "Item 3", Description = "Description 3" },
                new Item() { Name = "Item 4", Description = "Description 4" },
                new Item() { Name = "Item 5", Description = "Description 5" },
            };

            this.RemoveItemCmd = new DelegateCommand(OnRemoveItemCmd);
        }

        public ObservableCollection<Item> Items { get; set; }
        public DelegateCommand RemoveItemCmd { get; set; }

        private void OnRemoveItemCmd(object obj)
        {
            this.Items.Remove((Item)obj);
        }
    }

    public class ExtendedContextMenu : RadContextMenu
    {
        public static readonly DependencyProperty ClickedGridViewRowItemProperty =
            DependencyProperty.Register("ClickedGridViewRowItem", typeof(GridViewRow), typeof(ExtendedContextMenu), new PropertyMetadata(null));

        public GridViewRow ClickedGridViewRowItem
        {
            get { return (GridViewRow)GetValue(ClickedGridViewRowItemProperty); } 
            set { SetValue(ClickedGridViewRowItemProperty, value); }
        }

        protected override void OnOpened(RadRoutedEventArgs e)
        {
            base.OnOpened(e);
            this.ClickedGridViewRowItem = null;
            this.ClickedGridViewRowItem = this.GetClickedElement<GridViewRow>();
        }
    }
}
