B
B
bestdk22017-02-19 12:23:39
WPF
bestdk2, 2017-02-19 12:23:39

How to teach PreviewMouseDown to highlight items at the bottom of the treeview hierarchy?

Good afternoon.
What is there is a treeview with an attached HierarchicalDataTemplate. I want to make Multiselection. Found an example https://www.codeproject.com/Articles/24585/WPF-Mul... At first, the problem was that the author of the example uses TreeViewItem as tree elements, while I have my own custom class for elements ( TreeElem) and sender of SelectedItemChanged will receive an object of this class. At first, I wanted to get a TreeViewItem from it, but no matter what I tried to do, it didn't work. Then I decided to use some other event instead of SelectedItemChanged and PreviewMouseDown became it.
Here is the code in XAML:

<TreeView x:Name="PrjTreeViev"  
                      Margin="10,10,0,37" 
                      HorizontalAlignment="Left" 
                      Width="174" 
                      > 
<TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <EventSetter Event="PreviewMouseDown" Handler="SelectionChange" />
                    </Style>
 </TreeView.ItemContainerStyle>
 <TreeView.ItemTemplate >
         <HierarchicalDataTemplate ItemsSource="{Binding Path=Elems}" x:Name="Itemsq" >                
                        <StackPanel Orientation="Horizontal" AllowDrop="True" x:Name="panel">
                            <Image Source="{Binding IconSource}" Margin ="0 0 10 0"/>
                            <TextBlock Text="{Binding Name}"  />
                        </StackPanel>    
                    </HierarchicalDataTemplate>
 </TreeView.ItemTemplate>

And here, for starters, "SelectionChange:
void SelectionChange(object sender, MouseButtonEventArgs e)
{
            Debug.WriteLine("TEST+"+ e.OriginalSource);
            if (sender is TreeViewItem)
                {
                    Debug.WriteLine("TEST2+" + sender);
                    TreeViewItem treeviewItem = sender as TreeViewItem;
                    Debug.WriteLine("TEST3+" + (treeviewItem.Header as TreeElem).Name);
                 }
               e.Handled;
}

Here I just wanted to know what kind of elements stand out from me. It seems that they are defined correctly, but ... the expander does not work. As it turned out, but for some reason, in the preview with e.Handled, the expander does not want to be pressed into any. Then I slightly modified the code:
void SelectionChange(object sender, MouseButtonEventArgs e)
{
           
 if (!(e.OriginalSource is System.Windows.Shapes.Path)) // если нажатый объект экспендер - то отложим чутка e.Handled
 {
            if (sender is TreeViewItem)
                {
                    Debug.WriteLine("Что за тип +" + sender);
                    TreeViewItem treeviewItem = sender as TreeViewItem;
                    Debug.WriteLine("Имя выделенного элемента +" + (treeviewItem.Header as TreeElem).Name);
                 }
               e.Handled;
else
{
                System.Threading.Timer timer = null;
                timer = new System.Threading.Timer((obj) => 
                {
                    postponed(e);
                    timer.Dispose();
                },
                 null, 1000, System.Threading.Timeout.Infinite);

}
}
 private void postponed(MouseButtonEventArgs e)
{
            Debug.WriteLine("timer");
            e.Handled = true;
}

Now everything seems to be fine, the elements expand as needed, but ... When you click on an element one level lower in the hierarchy, even the highest one in the sender in the SelectionChange comes the element higher in the hierarchy of the one I clicked on.
How to fix it? Perhaps you shouldn't use Preview? But then what to use?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bestdk2, 2017-02-19
@bestdk2

Solved the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question