WPF中鼠标下A类控件的计数

发布于 2024-10-05 00:11:37 字数 2186 浏览 6 评论 0原文

我在画布上有一些自定义的 A 面板,其中还有 B 面板,我如何计数实际位于鼠标光标的 A 面板?

我知道这可以通过 VisualTreeHelper.HitTest 来实现,但机会不大,它总是返回自定义面板上的元素,或者根本不返回任何内容...

这是我的

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

代码 结果 == 0 我点击的任何地方...

alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-10-12 00:11:37

对于 HitTestResultCallback 中的每个命中,您可以尝试查找父 UserControl1 并将其添加到列表(如果尚未添加)

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文