C# WPF,树视图选择更改

C# WPF,树视图选择更改,c#,wpf,treeview,selection,C#,Wpf,Treeview,Selection,是否有方法捕获更改WPF树视图中当前选定项的尝试,并可能取消该尝试 treeview中的元素表示具有某些属性的页面。我想问用户,他是想放弃在页面上所做的更改,保存它们还是留在当前页面。您可能不会喜欢这个答案。。。WPFTreeView是个不友好的家伙。好的,首先要做的事 捕获更改所选项目的尝试: 最简单的方法是处理SelectedItemChanged事件: private void TreeView_SelectedItemChanged(object sender, RoutedPrope

是否有方法捕获更改WPF树视图中当前选定项的尝试,并可能取消该尝试


treeview中的元素表示具有某些属性的页面。我想问用户,他是想放弃在页面上所做的更改,保存它们还是留在当前页面。

您可能不会喜欢这个答案。。。WPF
TreeView
是个不友好的家伙。好的,首先要做的事

捕获更改所选项目的尝试:

最简单的方法是处理
SelectedItemChanged
事件:

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

为了满足您的要求,您还有很多工作要做。。。祝你好运。

了解更简单的解决方案。覆盖PreviewMouseDown,您将获得所需的结果

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

使用
PreviewMouseDown
是一半的解决方案。用户可以从键盘更改选择。使用
TreeViewItem。在所有树项目中选择

private TreeViewItem\u currenöTreeViewItem;
私人布尔树项目选择工作;
已选择私有无效树项(对象发送方、路由目标方)
{
如果(\u treeViewItemSelectedWork)
返回;
_treeViewItemSelectedWork=true;
TreeViewItem newTreeViewItem=(TreeViewItem)发送者;
如果(newTreeViewItem.IsSelected)
{
如果(_currenöTreeViewItem==null)
_currenöTreeViewItem=新的视图项;
如果(!等于(newTreeViewItem,当前树视图项))
{
如果(MessageBox.Show)(“撤消到上一个选择?”,“撤消”,MessageBoxButton.YesNo,
MessageBoxImage.None)=MessageBoxResult.Yes)
{
newTreeViewItem.IsSelected=false;
_currenöTreeViewItem.IsSelected=true;
}
其他的
{
_currenöTreeViewItem.IsSelected=false;
_currenöTreeViewItem=新的视图项;
_currenöTreeViewItem.IsSelected=true;
}
}
}
_treeViewItemSelectedWork=false;
}

是否有可能通过处理PreviewMouseDown(e.Handled=true)来实现这一点。这应该是公认的答案。简单,并且准确地解决了问题。如果你想使用鼠标以外的任何东西进行选择,这是行不通的。
 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;
        }
    }