문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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)
{
   ...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top