Search code examples
c#wpftreeviewselection

WPF, Treeview selection change


Is there a way to capture an attempt to change currently selected item in the WPF's TreeView and possibly cancel it?

Elements in the treeview represent pages with some properties. I would like to ask user if he wants to abandon changes made on the page, save them or stay in the current page.


Solution

  • Well you're probably not going to like the answer... the WPF TreeView is an unfriendly fellow. Ok, first things first...

    capturing an attempt to change the selected item:

    The easiest way to do this is to handle the SelectedItemChanged event:

    private void TreeView_SelectedItemChanged(object sender, 
    RoutedPropertyChangedEventArgs<object> e)
    {
        e.Handled = true;
    }
    

    Unfortunately, if you're using MVVM, then you'll need to handle this inside an Attached Property. Getting a bit more complicated now, if you're going to create an Attached Property to handle the SelectedItemChanged event, then you might as well implement a SelectedItem Attached Property that you could bind to in Two-Way Mode. I won't document how to do this here because there are plenty of online tutorials for this.

    ... and possibly cancel it:

    If you have a SelectedItem Attached Property, then you can monitor when that property changes. There is a catch of course... by the time the change comes into your view model, the UI has already changed. So, although you can stop the change from happening to the data in the view model, you cannot stop the selection being made in the UI.

    This is not a terrible problem though, because with a Two-Way Binding, you will be able to set the UI selection back to the previous item if necessary... take a look at this pseudo code:

    public YourDataType SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem != value)
            {
                if (selectedItem.HasChanges)
                {
                    if (WindowManager.UserAcceptsLoss()) 
                    {
                        selectedItem = value;
                        NotifyPropertyChanged("SelectedItem");
                    }
                    else ResetSelectedItem(selectedItem);
                }
                else 
                {
                    selectedItem = value;
                    NotifyPropertyChanged("SelectedItem");
                }
            }
        }
    }
    

    To fulfil your requirements, you have a lot of work ahead... good luck with that.