Answer the question
In order to leave comments, you need to log in
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>
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;
}
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question