Question

I want to add a click event handler when a WPF label is clicked. The idea is to do it code behind, because I´m generating the interface dynamically base on a document.

Was it helpful?

Solution

If I understand this correctly, you want a Label with a LeftMouseDown-event, which you would write from code?

In that case:

TestLabel.MouseLeftButtonDown += new MouseButtonEventHandler(TARGET);

OTHER TIPS

Since you've mentioned code behind, you can listen for the Label's PreviewMouseLeftButtonUp event since a Label control does not have a Click event associated with it.

Example:

var label = new Label();
label.PreviewMouseLeftButtonUp += LabelMouseLeftButtonUp;    

// Handler
private void LabelMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
   ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top