Allow edit of already selected item

Apr 1, 2014 at 8:44 AM
Hi,

Great bit of code. only issue I found was that once the time was too great after I selected an item I could never edit it without deselecting and re-selecting it.

I made the following change to the TreeViewPreviewMouseLeftButtonUp event TreeViewInPlaceEditBehavior to have it work how I wanted (like windows explorer).
private static void TreeViewPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var treeView = (TreeView)sender;
            var element = (UIElement)e.OriginalSource;

            var selectedItem = element.ParentOfType<TreeViewItem>();
            if (selectedItem == null)
            {
                ////We're clicking on nowhere, let's cancel the editing
                treeView.EndEdit();
                return;
            }

            var lastSelectedItem = GetLastSelectedItem(treeView);
            if (lastSelectedItem == null || lastSelectedItem != treeView.SelectedItem)
            {
                return;
            }

            var lastSelctedTime = GetLastSelectedTime(treeView);
            var interval = DateTime.Now.Subtract(lastSelctedTime).TotalMilliseconds;
            if (interval >= 400 && interval <= 1200)
            {
                ////It's long double click, consider it as a edit sign 
                treeView.BeginEdit();
            }
            else if (interval > 1200)
            {
                //reset the timer so we can edit an already selected item
                SetLastSelectedTime(treeView, DateTime.Now);
            }
        }