获得焦点事件处理所有文本框

时间:2015-02-20 05:19:37

标签: c# wpf

这是我的编码:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.PreviewMouseLeftButtonDownEvent,
           new MouseButtonEventHandler(SelectivelyHandleMouseButton), true);
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent,
          new RoutedEventHandler(SelectAllText), true);

        base.OnStartup(e);
    }

private static void SelectivelyHandleMouseButton(object sender, MouseButtonEventArgs e)
{
    var textbox = (sender as TextBox);
    if (textbox != null && !textbox.IsKeyboardFocusWithin)
    {
        if( e.OriginalSource.GetType().Name == "TextBoxView" )
        {
            e.Handled = true;
            textbox.Focus();
        }
    }
}

我收到错误:

  

onstartup()方法 - 方法无法覆盖

1 个答案:

答案 0 :(得分:1)

App.xaml中,您需要订阅活动启动:

<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml" Startup="Application_Startup">

    <Application.Resources>

    </Application.Resources>
</Application>

App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
{
    // Your code here
}