伪造WPF鼠标悬停可能吗?

Jip*_*ers 9 simulation wpf events mouseover

我有一个带有文本框的数据模板和一个带有一些样式的按钮.当焦点位于旁边的文本框上时,我希望按钮显示鼠标悬停状态.这可能吗?

我想它会涉及到这样的事情.我可以通过使用FindVisualChild和FindName来获取文本框.然后我可以在文本框上设置GotFocus事件来做某事.

_myTextBox.GotFocus += new RoutedEventHandler(TB_GotFocus);
Run Code Online (Sandbox Code Playgroud)

在TB_GotFocus这里我被卡住了.我可以得到我想要显示鼠标状态的按钮,但我不知道发送给它的是什么事件.不允许MouseEnterEvent.

void TB_GotFocus(object sender, RoutedEventArgs e)
  {
     ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(this.DataTemplateInstance);
     DataTemplate template = myContentPresenter.ContentTemplate;

     Button _button= template.FindName("TemplateButton", myContentPresenter) as Button;
     _button.RaiseEvent(new RoutedEventArgs(Button.MouseEnterEvent));

  } 
Run Code Online (Sandbox Code Playgroud)

Jes*_*det 2

我认为不可能伪造该事件,但您可以强制按钮呈现自身,就像它具有 MouseOver 一样。

private void tb_GotFocus(object sender, RoutedEventArgs e)
{
    // ButtonChrome is the first child of button
    DependencyObject chrome = VisualTreeHelper.GetChild(button, 0);
    chrome.SetValue(Microsoft.Windows.Themes.ButtonChrome.RenderMouseOverProperty, true);
}

private void tb_LostFocus(object sender, RoutedEventArgs e)
{
    // ButtonChrome is the first child of button
    DependencyObject chrome = VisualTreeHelper.GetChild(button, 0);
    chrome.ClearValue(Microsoft.Windows.Themes.ButtonChrome.RenderMouseOverProperty);
}
Run Code Online (Sandbox Code Playgroud)

您需要引用PresentationFramework.Aero.dlll才能使其工作,然后它仅适用于Vista的Aero主题。

如果您希望它适用于其他主题,您应该为您想要支持的每个主题制作一个自定义控件模板。

有关提示,请参阅http://blogs.msdn.com/llobo/archive/2006/07/12/663653.aspx