WPF TreeView -對已經選擇的項目強制選擇

[英]WPF TreeView - Force SelectedEvent on Item that is already selected

Summarized Question:
In the WPF TreeView, how can I force the selected event on an item that is currently the selected item?

總結問題:在WPF TreeView中,如何在當前選中的項上強制選擇事件?

Detailed Info:
I am trying to add functionality to my WPF TreeView by adding multiselect using shift(for a range) and control(for toggling selection of an item). I've implemented my own SelectedItems collection since the TreeView only has a selected item. I still use the SelectedItem of the TreeView so that it does not break keyboard functionality, however I do overwrite the existing ItemContainerStyle so that even though an item is selected, it does not give the appearance that it is selected. I wouldn't have needed to do this however, I needed to only highlight the background of the textblock of my selected treeview items, rather than the entire width of the item, so I handle the foreground and background color changes on my own.

詳細信息:我正在嘗試通過使用shift(用於范圍)和control(用於切換項目的選擇)向WPF TreeView添加功能。我實現了自己的SelectedItems集合,因為TreeView只有一個選定的項。我仍然使用TreeView的SelectedItem,這樣它不會破壞鍵盤功能,但是我確實覆蓋了現有的ItemContainerStyle,這樣即使選擇了一個項,它也不會給出它被選中的外觀。但是,我不需要這樣做,我只需要突出顯示所選treeview項的textblock的背景,而不是該項的整個寬度,所以我自己處理前景和背景顏色的更改。

1 个解决方案



Implementing a WPF treeview uses lots of XAML, so this answer contains just fragments of code.

實現WPF treeview需要大量的XAML,所以這個答案只包含代碼片段。

My goal was to click a selected treeview item in a left pane and refresh items in a right pane (like Windows Explorer).

我的目標是在左窗格中單擊選中的treeview項,並在右窗格中刷新項(如Windows Explorer)。

To make selecting a selected treeview item work, I implemented two events in the following XAML example in my ViewModel:


  • OnItemSelected using event TreeViewItem.Selected
  • OnItemSelected使用事件TreeViewItem.Selected
  • MouseLeftButtonUp using event TreeViewItem.MouseLeftButtonUp
  • MouseLeftButtonUp使用事件TreeViewItem.MouseLeftButtonUp

When my MouseLeftButtonUp event fires I make sure to indicate I handled the event using:


  • args.Handled = true;
  • arg游戲。處理= true;

Here is the XAML


<TreeView Name="MyTreeView" 
    ItemsSource="{Binding Collections}" 
        <Style TargetType="{x:Type TreeViewItem}">
            <EventSetter Event="TreeViewItem.MouseLeftButtonUp" Handler="MouseLeftButtonUp"/>
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />

    <!-- other XAML removed for this answer-->


Here are the event handlers


private void OnItemSelected(object sender, RoutedEventArgs e)
     // do something...

// additional info: cannot detect mouse down events; preview mouse events also work
private void MouseLeftButtonUp(object sender, MouseButtonEventArgs args)
   TreeViewItem tvi = sender as TreeViewItem;
   if (tvi != null)
      // process folder items
      MyViewModel fvm = tvi.Header as MyViewModel;
      if (fvm != null)
         // only process selected treeview items
         if (fvm.IsSelected)
            fvm.IsSelected = true;

            // prevent bubbling once we find the  selected tree view item
            args.Handled = true;



粤ICP备14056181号  © 2014-2021 ITdaan.com