如何识别哪一个元素的列表框在里面双击鼠标? [英] How to identify which element was double clicked inside a ListBox?

查看:108
本文介绍了如何识别哪一个元素的列表框在里面双击鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框。在列表框 的DataTemplate 由几个文本块有的文本框

I have a listbox. The listbox DataTemplate consists of few Text Blocks and some TextBoxes.

的问题是鼠标双击我需要找出哪些元素双击已完成,这样做一些进一步的操作,例如使文本框编辑等。同时我需要定义列表中双击也有所行动。所以我不能对每个组件处理鼠标分别下降

The issue is on mouse double click I need to find out on which element the double click was done, so as to do some further operations like make the TextBox editable and so on. At the same time I need to define some action for list double click also. So I can't handle mouse down separately for each component.

因此,我需要向下处理鼠标的的ListBox ,并找出哪些元素双击制成。

Hence, I need to handle the mouse down for the ListBox and find out on which element the double click was made.

我试着用下面的代码,但它返回的的ListBox的名称而不是文本框 取值名称

I tried with below code, but it returns the ListBox's Name instead of TextBox's Name:

private void myListBox_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{    
    var mouseWasDownOn = e.Source as FrameworkElement;
    if (mouseWasDownOn != null)
    {
        string elementName = mouseWasDownOn.Name;
    }
}



我也尝试按以下问题

WPF获取元素鼠标下(S)

如何知道什么控制鼠标点击了一个帆布?

<一个HREF =http://stackoverflow.com/questions/16525774/getting-the-logical-uielement-under-the-mouse-in-wpf>获取在WPF鼠标下的逻辑的UIElement

public void ListBox_MouseDownHandler(object sender, MouseButtonEventArgs e)
{
    HitTestResult target = VisualTreeHelper.HitTest(myListBoxName, e.GetPosition(myListBoxName));
    while(!(target is Control) && (target != null))
    {
        target = VisualTreeHelper.GetParent(target);
    }
}



但还是没能找到解决的办法。因此,请大家帮我摆脱双击元素类型或元素的名称。

But still could not find a solution. Hence please help me to get the element type or element name from double click.

推荐答案

您可以使用的 FrameworkTemplate.FindName方法(String ,FrameworkElement的) 为此,它应该工作,只要你想:

You can use FrameworkTemplate.FindName Method (String, FrameworkElement) for this purpose and it should works as you want:

private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is childItem)
            return (childItem)child;
        childItem childOfChild = FindVisualChild<childItem>(child);
        if (childOfChild != null)
            return childOfChild;
    }
    return null;
}



然后:

Then:

private void LstBox_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListBoxItem ListBoxItem = (ListBoxItem)(lstBox.ItemContainerGenerator.ContainerFromIndex(lstBox.SelectedIndex));
    ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(ListBoxItem);
    DataTemplate myDataTemplate = contentPresenter.ContentTemplate;
    StackPanel temp = (StackPanel)myDataTemplate.FindName("myStackPanel", contentPresenter);
    //*so as to do some further operations like make the textbox editable and so on* as you want
   (temp.FindName("field1TextBox") as TextBox).IsReadOnly = false;
}

根据你的问题,你说的列表框的DataTemplate 由几个的TextBlock 有的文本框 的。 (我以为他们是在的StackPanel

Based on your question that you said: The listbox's DataTemplate consists of few TextBlock and some TextBoxes. (I assumed they are inside a StackPanel)

这篇关于如何识别哪一个元素的列表框在里面双击鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆