星期三, 三月 16, 2011

How to deselect all selected items in a WPF treeview when clicking on some empty area? - Stack Overflow

How to deselect all selected items in a WPF treeview when clicking on some empty area? - Stack Overflow

protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);

DependencyObject dpSource = e.OriginalSource as DependencyObject;

if (dpSource.FindVisualAncestor(o => typeof(TreeViewItem).IsAssignableFrom(o.GetType())) == null)
UnselectAll();
}


private void MyTreeView_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
if ((sender as TreeViewItem) == null) {
TreeViewItem item = MyTreeView.SelectedItem as TreeViewItem;
if(item != null){
item.IsSelected = false;
}
}
}

没有评论:

发表评论