命中测试...是否可以测试列表框或其他项目控件中的项目

发布于 2024-08-03 09:55:45 字数 100 浏览 6 评论 0原文

我试图点击列表框中的测试项目但无济于事。是否可以? VisualTreeHelper 显示 x,y 坐标,其中每个项目都返回 0,0,其中似乎排除了命中测试。有谁知道解决方案或解决方法?

I'm trying to hit test items within a listbox but to no avail. Is it possible? The VisualTreeHelper shows the x,y coordinates returning 0,0 for each and every item within which would seem to rule hit testing out. Does anyone know of a solution or workaround?

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-08-10 09:55:45

你尝试过什么?您是否尝试过 UIElement.InputHitTest方法? VisualTreeHelper.HitTest方法?您是否已通读本文

根据您的问题,我猜您正在致电 VisualTreeHelper.GetOffset?如果是这样,则偏移量是相对于父级的,很可能是 0,0。

您需要向我们提供更多信息来帮助您。

What have you tried? Have you tried the UIElement.InputHitTest method? The VisualTreeHelper.HitTest method? Have you read through this?

Based on your question I guess you're calling VisualTreeHelper.GetOffset? If so, the offset is relative to the parent, which may very well be 0,0.

You need to give us more information with which to help you.

星軌x 2024-08-10 09:55:45

试试这个:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    var hti = listBox1.IndexFromPoint(e.X, e.Y);
    if (hti==-1)
    {
        //user didn't click on an item
    }
    else
    {
        //user clicked on an item
        if (hti==listBoxFolder.SelectedIndex)
        {
            //user clicked on the item that was already selected
        }
        else
        {
            //user clicked on an item that was not already selected
        }
    }
}

假设 listbox1.SelectionMode 设置为 SelectionMode.One(这是默认设置)。

Try this:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    var hti = listBox1.IndexFromPoint(e.X, e.Y);
    if (hti==-1)
    {
        //user didn't click on an item
    }
    else
    {
        //user clicked on an item
        if (hti==listBoxFolder.SelectedIndex)
        {
            //user clicked on the item that was already selected
        }
        else
        {
            //user clicked on an item that was not already selected
        }
    }
}

This assumes that listbox1.SelectionMode is set to SelectionMode.One (which is the default setting).

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