0

I have some custom A panels on a canvas, where there a also B panels, how can I count A panels located the mouse cursor actually?

I know that this could be achieved with VisualTreeHelper.HitTest, but didn't have much chance, it returns always the elements on the custom panels or nothing at all...

this is my code

<UserControl x:Class="WpfApplication7.UserControl1">
    <Grid>
        <Label Content="Label" Height="44" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="78" Background="#FF4B9FC4" BorderBrush="#FF020A0D" BorderThickness="1" />
    </Grid>
</UserControl>

<Window x:Class="WpfApplication7.MainWindow"
PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown" xmlns:my="clr-namespace:WpfApplication7">
    <Grid>
        <my:UserControl1 HorizontalAlignment="Left" Margin="82,88,0,0" x:Name="userControl11" VerticalAlignment="Top" />
        <my:UserControl1 HorizontalAlignment="Left" Margin="168,166,0,0" x:Name="userControl12" VerticalAlignment="Top" />
        <my:UserControl1 HorizontalAlignment="Left" Margin="231,130,0,0" x:Name="userControl13" VerticalAlignment="Top" />
    </Grid>
</Window>

.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    List<UserControl1> ucs = new List<UserControl1>();

    private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        GetUcsCount(e);
        Console.WriteLine("ucs.Count = {0}", ucs.Count);
    }

    private void GetUcsCount(MouseButtonEventArgs e)
    {
        ucs.Clear();

        Point p = e.GetPosition(this);

        VisualTreeHelper.HitTest(this, null, 
            new HitTestResultCallback(MyHitTestCallback), 
            new PointHitTestParameters(p));
    }

    HitTestResultBehavior MyHitTestCallback(HitTestResult result)
    {
        if (result.VisualHit.GetType() == typeof(UserControl1))
        {
            ucs.Add(result.VisualHit as UserControl1);
        }

        return HitTestResultBehavior.Continue;
    }
}

the result == 0 anywhere I click...

alt text

serhio
  • 28,010
  • 62
  • 221
  • 374

1 Answers1

1

For each hit in the HitTestResultCallback you can try to find the parent UserControl1 and add it to the list if it hasn't been added yet

HitTestResultBehavior MyHitTestCallback(HitTestResult result)
{
    DependencyObject visualHit = result.VisualHit;
    UserControl1 parentUserControl = GetVisualParent<UserControl1>(visualHit);
    if (parentUserControl != null && ucs.IndexOf(parentUserControl) < 0)
    {
        ucs.Add(parentUserControl as UserControl1);
    }
    return HitTestResultBehavior.Continue;
}
public static T GetVisualParent<T>(object childObject) where T : Visual
{
    DependencyObject child = childObject as DependencyObject;
    while ((child != null) && !(child is T))
    {
        child = VisualTreeHelper.GetParent(child);
    }
    return child as T;
}
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • thank you, I observerd this too, but now it returns too much elements :( not only under the mouse – serhio Dec 01 '10 at 12:34
  • @serhio: I can't find any situation where I get an element in the FilterCallback that's not under the mouse. When does this happend? – Fredrik Hedblad Dec 01 '10 at 12:54
  • @Meleak: I understood why, in fact myusercontrol was transparent, but large, and only childs elements where really visible.. now, i search a method taht will count the UserControls after the hit on child not on the transparent part... – serhio Dec 01 '10 at 13:49
  • @serhio: Updated my answer, if I understood correctly this should get you what you want – Fredrik Hedblad Dec 01 '10 at 14:02
  • @Meleak: I uploaded an image for better understanding the situation, the blue part is transparent in my situation, this why i got 2 controls instead of 1 whet hittesting – serhio Dec 01 '10 at 14:37
  • and what about **MyHitTestFilterCallback**? – serhio Dec 01 '10 at 14:41
  • Doint this, you won't need it :) Just pass null as parameter as you did before. This will only find the parent UserControl1 if a child-control was actually hit at the click point and not if you click a "transparent" part of the UserControl1. I tried it with 4 partially overlapping UserControl1's and got the expected 0 to 4 hits depending on where I clicked. – Fredrik Hedblad Dec 01 '10 at 14:44