gpt4 book ai didi

c# - WPF, TreeView 选择更改

转载 作者:太空狗 更新时间:2023-10-29 21:09:17 25 4
gpt4 key购买 nike

有没有办法捕获更改 WPF 的 TreeView 中当前选定项目的尝试并可能取消它?

TreeView 中的元素表示具有某些属性的页面。我想询问用户是否要放弃在页面上所做的更改、保存它们或留在当前页面。

最佳答案

对于更简单的解决方案。覆盖 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;
}
}

关于c# - WPF, TreeView 选择更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com