开发者

count controls of type A under the mouse in WPF

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);
        Conso开发者_高级运维le.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...

count controls of type A under the mouse in WPF


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜