WPF TreeView清除选择

时间:2009-03-24 10:30:51

标签: .net wpf treeview

如何在WPF TreeView中清除TreeView选项?我尝试循环遍历TreeNodes并清除IsSelected属性,但这是ReadOnly属性。有什么想法吗?

TreeView正在通过XMLDataProvider对象使用XML绑定。

9 个答案:

答案 0 :(得分:11)

我遇到了完全相同的问题并编写了以下代码,这些代码适用于任何树视图,只需要对第一个函数进行一行调用。

class TomWrightsUtils
{
    public static void ClearTreeViewSelection(TreeView tv)
    {
        if (tv != null)
            ClearTreeViewItemsControlSelection(tv.Items, tv.ItemContainerGenerator);
    }
    private static void ClearTreeViewItemsControlSelection(ItemCollection ic, ItemContainerGenerator icg)
    {
        if ((ic != null) && (icg != null))
            for (int i = 0; i < ic.Count; i++)
            {
                TreeViewItem tvi = icg.ContainerFromIndex(i) as TreeViewItem;
                if (tvi != null)
                {
                    ClearTreeViewItemsControlSelection(tvi.Items, tvi.ItemContainerGenerator);
                    tvi.IsSelected = false;
                }
            }
    }
}

答案 1 :(得分:8)

不确定TreeNodes是什么意思。

通常,您的视图模型上会有一个对应的IsSelected属性,您的视图会绑定到该属性:

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

因此,您只需遍历视图模型中的数据项并在那里设置IsSelected = false

然而,听起来你没有这样的财产。在这种情况下,您需要为每个数据项获取相应的TreeViewItem。有关如何执行此操作的信息,请参阅TreeView.ItemContainerGenerator属性。类似的东西:

var treeViewItem = _treeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
treeViewItem.IsSelected = false;

答案 2 :(得分:3)

找到所选项目并设置值:

private void Button_Click(object sender, RoutedEventArgs e)
{
  TreeViewItem tvi = treeviewExample.SelectedItem as TreeViewItem;
  if (tvi != null)
  {
    tvi.IsSelected = false;
  }
}

答案 3 :(得分:2)

这可以作为一种扩展方法,因此您可以调用

youTreeview.ClearSelection();

using System.Windows.Forms;
using System.Windows.Controls;

namespace YourAppNamespace
{
    public static void ClearSelection(this TreeView input)
    {
    // this should be some container that you put in
    // possibly the actual treeviewitem, not sure on that though
    var selected = input.SelectedItem;
    if (selected == null)
        return;

    // in my case this works perfectly
    var tvi = input.ItemContainerGenerator.ContainerFromItem(selected) as TreeViewItem;
    var tvi = input.ItemContainerGenerator.ContainerFromItem(selected) as TreeViewItem;
    if (tvi == null)
    {
        // it must be a child, heres a hack fix
        // my nodes are inherited from TreeViewItemViewModel by Josh Smith
        var child = selected as WPF.Controls.TreeViewItemViewModel;
        if (child == null)
            return;
        child.IsSelected = false;

    }
    else
        tvi.IsSelected = false;
     }


}

答案 4 :(得分:1)

我的经验是远离标准的ItemContainerGenerator调用,因为它们将在深度大于n + 1的节点上失败。我使用以下扩展方法的组合。 ContainerFromItem扩展方法来自MSDN博客,它在处理TreeView时为我创造了奇迹。

  public static void ClearSelection(this TreeView input)
  {
     var selected = input.SelectedItem;

     if (selected == null) return;

     var tvi = input.ContainerFromItem(selected) as TreeViewItem;

     if (tvi == null) return;

     tvi.IsSelected = false;

  }

  public static TreeViewItem ContainerFromItem(this TreeView treeView, object item)
  {
     TreeViewItem containerThatMightContainItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);
     if (containerThatMightContainItem != null)
        return containerThatMightContainItem;
     else
        return ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
  }

  private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
  {
     foreach (object curChildItem in itemCollection)
     {
        TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
        if (parentContainer == null)
           return null;
        TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
        if (containerThatMightContainItem != null)
           return containerThatMightContainItem;
        TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
        if (recursionResult != null)
           return recursionResult;
     }
     return null;
  }

答案 5 :(得分:1)

TreeViewItem tvi = tvMain.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;

if (tvi != null) { tvi.IsSelected = true; tvi.IsSelected = false; }

答案 6 :(得分:0)

这似乎到目前为止工作,但我只是在5分钟前把它放进去,所以使用风险自负。我基本上想要在用户在树控件中单击时清除选择,而不是在树项上。

 void DestinationTree_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     TreeView tree = sender as TreeView;
     DestinationClientViewModel selectedItem = tree.SelectedItem as DestinationClientViewModel;

     if (selectedItem != null)
     {
        int selectedItemIndex = this.DestinationTree.Items.IndexOf(selectedItem);

        if (selectedItemIndex > -1)
        {
            TreeViewItem tvi = this.DestinationTree.ItemContainerGenerator.ContainerFromIndex(selectedItemIndex) as TreeViewItem;
            if (tvi != null)
                tvi.IsSelected = false;
        }
     }
 }

答案 7 :(得分:0)

在我找到一个对我有用的解决方案很久以后,我自己使用自定义的Tree List View实现了这种情况。

完整说明可在http://social.msdn.microsoft.com/Forums/vstudio/en-US/36aca7f7-0b47-488b-8e16-840b86addfa3/getting-treeviewitem-for-the-selected-item-in-a-treeview

找到

基本思想是捕获TreeViewItem.Selected事件并将事件源保存到TreeView上的Tag属性中。然后,当您需要清除它时,可以访问控件上的Tag属性并将IsSelected值设置为False。这适用于2级嵌套子级。希望它对你有用。

为了坚持:

TreeView声明

  <TreeView Name="myTreeView" TreeViewItem.Selected="OnItemSelected"
    ItemsSource="{Binding Source={StaticResource myHierarchicalData}}"/>

事件处理程序

private void OnItemSelected(object sender, RoutedEventArgs e)
{
    myTreeView.Tag = e.OriginalSource;
}

清除选择逻辑

if (myTreeView.SelectedItem != null)
{
    TreeViewItem selectedTVI = myTreeView.Tag as TreeViewItem;
    // add your code here mine was selectedTVI.IsSelected = false;
}

答案 8 :(得分:0)

我刚遇到同样的问题。

快速而肮脏的解决方案

tree.ItemsSource = null;

tree.ItemsSource = yourSource;