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 个解决方案

#1


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:

為了讓選擇的treeview項工作,我在我的ViewModel中實現了以下XAML示例中的兩個事件:

  • 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:

當我的MouseLeftButtonUp事件觸發時,我確保使用:

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

Here is the XAML

這是XAML

<TreeView Name="MyTreeView" 
    ItemsSource="{Binding Collections}" 
    Margin="0"                           
    Grid.Row="0"
    TreeViewItem.Selected="OnItemSelected"
    TreeViewItem.Unselected="OnItemUnSelected">
    <TreeView.ItemContainerStyle>
        <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}" />
        </Style>
    </TreeView.ItemContainerStyle>

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

</TreeView>

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;
         }
      }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/18/72f33a29777f67aa2e6e5364cc42b6c4.html



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