Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 461
Spook Avatar asked Nov 27 '13 14:11

Spook


People also ask

How to set Selected Item in TreeView WPF?

We can usually set TreeView's selected item by binding. In this case, if you need to set an item in the list as a selected item, you only need to set the corresponding Model. IsSelected to True.

What is TreeView in WPF?

A TreeViewItem control is a HeaderedItemsControl that has a Header and an Items collection. If you are defining a TreeView by using Extensible Application Markup Language (XAML), you can explicitly define the Header content of a TreeViewItem control and the items that make up its collection.


2 Answers

For a much simpler solution. Override the PreviewMouseDown and you will get the desired result.

 private void Tree_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        // first did the user click on a tree node?
        var source = e.OriginalSource as DependencyObject;
        while (source != null && !(source is TreeViewItem))
            source = VisualTreeHelper.GetParent(source);
        source = source as TreeViewItem;
        if (source == null) return;

        var treeView = sender as TreeView;
        if (treeView == null) return;

        // validate our current item to decide if we allow the change
        // or do whatever checks you wish
        if (!ItemIsValid(treeView.SelectedItem))
        {
            // it's not valid, so cancel the attempt to select an item.
            e.Handled = true;
        }

        // Maybe you want to check the about to be selected value?
        MyClass data = source.DataContext;
        if (!CanSelect(data))
        {
            // we can't select this, so cancel the attempt to select.
            e.Handled = true;
        }
    }
like image 124
JB. Avatar answered Oct 16 '22 07:10

JB.


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.

like image 26
Sheridan Avatar answered Oct 16 '22 09:10

Sheridan