简体   繁体   English

如何使用AddHandler在WPF中的按钮上为LostKeyBoardFocusEvent添加事件处理程序

[英]How to use AddHandler to add event handler for a LostKeyBoardFocusEvent on a button in WPF

I am trying to encapsulate some button behaviors and one of which is to add an event handler for event "LostKeyboardFocusEvent", but I got "handler type is mismatched" error in run-time with and SystemArgumentException: 我正在尝试封装一些按钮行为,其中之一是为事件“ LostKeyboardFocusEvent”添加事件处理程序,但是在运行时使用和SystemArgumentException出现“处理程序类型不匹配”错误:

AssociatedObject.AddHandler(Button.LostKeyboardFocusEvent, new DependencyPropertyChangedEventHandler(HandleButtonToolTip), true);

public static void HandleButtonToolTip(object sender, DependencyPropertyChangedEventArgs e)
        {
            //Get tooltip from sender.
            ToolTip tt = (ToolTip)(sender as Control).ToolTip;
            if ((sender as Control).IsKeyboardFocusWithin && !tt.IsOpen)
            {
                ShowButtonTooltip(sender);
            }
            else
            {
                ClearButtonTooltip();
            }
        }

Any help is appreciated! 任何帮助表示赞赏!

Change 更改

AssociatedObject.AddHandler(Button.LostKeyboardFocusEvent, new DependencyPropertyChangedEventHandler(HandleButtonToolTip), true);

to

AssociatedObject.AddHandler(Button.LostKeyboardFocusEvent, new RoutedEventHandler(HandleButtonToolTip), true);

And change 并改变

public static void HandleButtonToolTip(object sender, DependencyPropertyChangedEventArgs e)

to

public static void HandleButtonToolTip(object sender, RoutedEventArgs e)

Just change the type of the handler from DependencyPropertyChangedEventArgs to KeyboardFocusChangedEventHandler : 只需将处理程序的类型从DependencyPropertyChangedEventArgs更改为KeyboardFocusChangedEventHandler

AssociatedObject.AddHandler(Button.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(HandleButtonToolTip), true);

...and the type of the EventArgs to KeyboardFocusChangedEventArgs : ...以及EventArgs的类型为KeyboardFocusChangedEventArgs

public static void HandleButtonToolTip(object sender, KeyboardFocusChangedEventArgs e)
...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM