WPF, Treeview selection change

送分小仙女□ 提交于 2019-12-23 09:04:09

问题


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.


回答1:


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.




回答2:


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;
        }
    }


来源:https://stackoverflow.com/questions/20244916/wpf-treeview-selection-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!