簡體   English   中英

如何使用AddHandler在WPF中的按鈕上為LostKeyBoardFocusEvent添加事件處理程序

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

我正在嘗試封裝一些按鈕行為,其中之一是為事件“ 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();
            }
        }

任何幫助表示贊賞!

更改

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

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

並改變

public static void HandleButtonToolTip(object sender, DependencyPropertyChangedEventArgs e)

public static void HandleButtonToolTip(object sender, RoutedEventArgs e)

只需將處理程序的類型從DependencyPropertyChangedEventArgs更改為KeyboardFocusChangedEventHandler

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

...以及EventArgs的類型為KeyboardFocusChangedEventArgs

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM