문제

This code works

TextBlock tbTest = new TextBlock();
tbTest.MouseRightButtonDown += new MouseButtonEventHandler(cc_CopyToClip);

But I need to do the same thing with a SetValue
This does not work - compiler error

FrameworkElementFactory textblock = new FrameworkElementFactory(typeof(TextBlock));
textblock.SetValue(TextBlock.MouseRightButtonDownEvent, += new MouseButtonEventHandler(cc_CopyToClip));

How to assign an event handler via SetValue?

Answer

textblock.AddHandler(TextBlock.MouseRightButtonDownEvent, new MouseButtonEventHandler(cc_CopyToClip));
도움이 되었습니까?

해결책 2

It is not dependency property to use SetValue. You could use AddHandler to add routed event handler.

다른 팁

To assign/unassign routed event handler FrameworkElementFactory has AddHandler and RemoveHandler methods. So your call should look like this:

textblock.AddHandler(TextBlock.MouseRightButtonDownEvent, new MouseButtonEventHandler(cc_CopyToClip));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top