using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Telerik.UI.Xaml.Controls.Primitives;
using Telerik.UI.Xaml.Controls.Primitives.Menu;
using Windows.UI.Core;
using Windows.UI.Xaml;

namespace App61
{
    public class CustomCommand : ICommand
    {
        private DispatcherTimer timer;

        public CustomCommand()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            var context = parameter as RadialMenuItemContext;
            var target = context.TargetElement;

            var behavior = RadRadialContextMenu.GetBehavior(target) as CustomTriggerBehavior;

            var menu = RadRadialContextMenu.GetMenu(target);

            menu.IsOpen = false;
            timer.Tick += (s, e) =>
            {
                timer.Stop();
                behavior.Detach();
            };
            timer.Start();
        }
    }
}
