简体   繁体   English

如何获取触发MouseDoubleClick事件的UIElement?

[英]How do I get the UIElement that triggered a MouseDoubleClick Event?

I'm working on a simple IM program as a personal project, and I've hit a bit of a snag. 我正在作为个人项目开发一个简单的IM程序,但遇到了一些麻烦。 It's really more of a cosmetic thing, but I'm having some trouble with it. 实际上,这实际上是一件装饰性的事情,但是我遇到了一些麻烦。 I've got a sidebar that lists all of a user's contents in the main window, and I'd like to set it up so that when a user clicks on a contact name, a tab opens in the chat area of the main window with a chat session opened with that contact. 我有一个侧边栏,它在主窗口中列出了用户的所有内容,并且我希望对其进行设置,以便在用户单击联系人姓名时,在主窗口的聊天区域中打开一个选项卡,其中包含与该联系人打开的聊天会话。 The really important part of this is for me to be able to get the UIElement, in this case a Label, that kicked off the MouseDoubleClick event. 对于我来说,真正重要的部分是能够获取启动MouseDoubleClick事件的UIElement(在本例中为Label)。 Once I can access this, I can access the information that I need to make the connection. 一旦可以访问它,就可以访问建立连接所需的信息。 Unfortunately, I'm a bit rusty with mouse events, and can't figure out how to get back to the Label once the event has been fired. 不幸的是,我对鼠标事件有些生疏,无法弄清楚一旦事件被触发后如何回到Label。 My source code for programmatically creating the label is as follows: 我以编程方式创建标签的源代码如下:

foreach (ContactInfo contact in ContactList)
{
    Label currentContact = new Label();
    currentContact.Content = contact.ContactName.ToString() + " (" + contact.MachineName.ToString() + ")";
    currentContact.MouseDoubleClick += new MouseButtonEventHandler(ContactDoubleClickHandler);
    StckPnl_Contacts.Children.Add(currentContact);
}

And the (currently empty) handler is this: (当前为空)处理程序是这样的:

public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
{

}

Am I going about this the wrong way? 我会以错误的方式处理吗? Any help would be appreciated. 任何帮助,将不胜感激。

You can inspect the sender (first casting it to the type) to get the element that triggered the event: 您可以检查sender (首先将其转换为类型)以获取触发事件的元素:

Label targetLabel = sender as Label;
if (targetLabel != null)
{
    // Do something. I recommend not doing a direct cast in case someone in the future hooks another control type to the event handler.
}

You can use either of following to access the sender details 您可以使用以下任一方式访问发件人详细信息

public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
    {
       var uiElement = (UIElement) sender; // cast it to UIElement
    }


public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
    {
       var dp = (DependencyObject) sender; // cast it to dependency object.
    }

Actually, the sender is your Label , you just have to transform it using: 实际上,发件人是您的Label ,您只需要使用以下方法进行转换:

Label contact = sender as Label;

Be sure to check if contact is null though, before performing any further operations. 但是,在执行任何进一步的操作之前,请务必检查contact是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ControlTemplate中引用顶部UIElement? - How do I reference top UIElement in ControlTemplate? 如何在C#上将MouseDoubleClick事件添加到TreeViewItem - How to add a MouseDoubleClick event on C# to a TreeViewItem 如何获取收到mousedoubleclick的应用程序的句柄? - How to get the handle of application that received the mousedoubleclick? 属性更改时如何更改 UIElement? - How do i make a UIElement change when a propertie change? 如何通过两种方式将Parent UIElement绑定到附加属性? - How do I two way bind a Parent UIElement to an attached property? 如何获取CSLA 3.02 BusinessListBase ListChanged事件以标识其OnPropertyChanged触发了该事件的子对象? - How do you get CSLA 3.02 BusinessListBase ListChanged event to identify the child object whose OnPropertyChanged triggered the event? 在MouseDoubleClick事件上刷新绑定的DataGrid - Refresh a Bound DataGrid on a MouseDoubleClick event 我无法获得UIElement的ActualSize - I can't get ActualSize of an UIElement 如何从ListView中的模板数据中获取UIElement? - How to get UIElement out of templated data in ListView? 如何触发和捕获DataGridViewComboBoxCell.MouseDoubleClick事件并从此事件中打开另一个表单? - How to fire & catch DataGridViewComboBoxCell.MouseDoubleClick event and open another form from this event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM