wpf - Hittesting vs2008 - Not all visuals found in treeview

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
R

Rolf Welskes

Hello,
because there are no managed news group for window presentation foundation
and one has told me I can use this, here my problem:

simple example:

<Window x:Class="TreeViewHitTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="354" Width="644" >
<Grid Background="LightGray">
<TreeView AllowDrop="True" Background="LightBlue"
HorizontalAlignment="Left" Margin="10,10,0,71" Name="treeView" Width="200"
PreviewMouseDown="grid01_PreviewMouseDown">
<TreeViewItem Header="aaa01"/>
<TreeViewItem Header="aaa02"/>
<TreeViewItem Header="aaa03" />
</TreeView>
<ListBox Margin="286,56,138,71" Name="lbxResult" />
</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TreeViewHitTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

public HitTestResultBehavior HitTestResultFunc(HitTestResult result)
{
DependencyObject dobj = result.VisualHit;
lbxResult.Items.Add(dobj.GetType().Name);
return HitTestResultBehavior.Continue;
}



private void grid01_PreviewMouseDown(object sender,
MouseButtonEventArgs e)
{
lbxResult.Items.Clear();

VisualTreeHelper.HitTest(treeView , null, HitTestResultFunc, new
PointHitTestParameters(e.GetPosition(treeView)));

}
}
}

So if you start the program and left click on a TreeViewItem in the
treeview, you see the hittesting starts
at TextBlock, this seems ok, but then it travers the visual tree and does
not reach each element,
ESPESPIALY I need the TreeViewItem which is clicked, but this is not in the
list of the items found hittesting.

Thank you for any help.
Rolf Welskes
 
Hi Rolf,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Rolf,

Not all visuals will be returned in the HitTestResultCallback, instead, we
should use HitTestFilterCallback if we need to know every potential visual
that is hit (in z-order).

If you use following code:

public HitTestResultBehavior HitTestResultFunc(HitTestResult result)
{
DependencyObject dobj = result.VisualHit;
// lbxResult.Items.Add(dobj.GetType().Name);
return HitTestResultBehavior.Continue;
}

public HitTestFilterBehavior HitTestFilterFunc(DependencyObject
potentialHitTestTarget)
{
lbxResult.Items.Add(potentialHitTestTarget.GetType().Name);
return HitTestFilterBehavior.Continue;
}


private void grid01_PreviewMouseDown(object sender,
MouseButtonEventArgs e)
{
lbxResult.Items.Clear();
VisualTreeHelper.HitTest(treeView, HitTestFilterFunc,
HitTestResultFunc, new
PointHitTestParameters(e.GetPosition((UIElement)sender)));


You can see the TreeViewItem is returned correctly.

Another workaround is to use the HitTest to return a DependencyObject, then
walk up the visual tree until you find a TreeViewItem in the TreeView
you're checking.

You can use VisualTreeHelper.GetParent() to return the parent.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,
thank you this is a work around.
BUT: I have also tried to override TreeViewItem.HitTestCore().
This method is not called while hittesting.
Furthermore, when I set a filter for hittesting, then what I define as
elemnts for hittesting should be in the Hittestresult,
but is not.

All togehter: Hittesting in wpf is complex (compared to windows forms) and
it is not helpfull, that the hittest-behaver is so,
that no one knows what is hitested in the tree and what is not, so the
programmer is blind because he/she does not know
what realy happens.

Ok, it works.

So, thank you for you help
and best regards
Rolf Welskes
 
Back
Top