简体   繁体   English

如何在树形视图中取消选择所选项目

[英]How to deselect the selected item in tree view

I am developing a WPF application, where user has the option to navigate between the pages by clicking next / previous button or by selecting a particular page in the tree view. 我正在开发WPF应用程序,用户可以通过单击下一个/上一个按钮或通过在树状视图中选择特定页面来在页面之间导航。

<TreeView Background="Transparent" Cursor="Hand" ItemsSource="{Binding Path=TreeItems}" local:TreeViewExtensions.SelectedItemChanged="{Binding Path=SelectedItemChangedCommand}" ItemContainerStyle="{StaticResource TreeViewItemStyle}" >
     <TreeView.ItemTemplate>
          <DataTemplate>
               <TextBlock Text="{Binding DisplayName}" />
          </DataTemplate>
     </TreeView.ItemTemplate>
</TreeView>
<Button Content="&lt; Prev" Name="btnPrev" Command="{Binding Path=MovePreviousCommand}"/>
<Button Content="Next &gt;" Name="btnNext" Command="{Binding Path=MoveNextCommand}"/>

Now the problem is, when a user clicks on a particular page name, application will navigate to particular page. 现在的问题是,当用户单击特定页面名称时,应用程序将导航到特定页面。 If the user clicks next or previous it be navigated to next or previous page. 如果用户单击下一页或上一页,则将其导航到下一页或上一页。 In this scenario if user clicks on the previously selected item of treeview, it will not navigate to that particular page as it is already selected. 在这种情况下,如果用户单击树形视图的先前选择的项目,它将不会导航到该特定页面,因为它已经被选择。

Can anyone let me know, how to deselect the selected item of tree view when user clicks on 'Next' or 'Prev' button. 谁能让我知道,当用户单击“下一步”或“上一步”按钮时,如何取消选择树状视图的选定项。

Use something like this to deselect selected items 使用类似的方法取消选择选定的项目

<TreeView MouseLeftButtonDown="TreeView_MouseLeftButtonDown">
    <TreeViewItem Header="Employee1">
        <TreeViewItem Header="Jesper"/>
        <TreeViewItem Header="Aaberg"/>
        <TreeViewItem Header="12345"/>
    </TreeViewItem>
    <TreeViewItem Header="Employee2">
        <TreeViewItem Header="Dominik"/>
        <TreeViewItem Header="Paiha"/>
        <TreeViewItem Header="98765"/>
    </TreeViewItem>
</TreeView>

On Click of Prev or Next, you can set the selectedItem value for the treeView node to true and set focus on this node. 在单击“上一个”或“下一个”时,可以将treeView节点的selectedItem值设置为true,并将焦点设置在此节点上。 The previously selected nodes, unselected event shall get fired. 先前选择的节点,未选择的事件将被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM