WPF TextBox 的 EventTrigger & 重写控件

遇到一个需求,在textbox获得焦点的时候,调用一个外部的软键盘。

这可以用两个不同的方法来达到目的。

1、EventTrigger

首先定义一个Style

<Style x:Key="TopSoftKeyboardTextBox" TargetType="{x:Type TextBox}">

        <Setter Property="FontFamily" Value="黑体"/>

        <Setter Property="FontSize" Value="24"/>

        <EventSetter Event="UIElement.GotFocus" Handler="SoftKeyboardTextBoxGotFocusTop"></EventSetter>

        <EventSetter Event="UIElement.LostFocus" Handler="SoftKeyboardTextBoxLostFocus"></EventSetter>

    </Style>

然后定义两个方法

private void SoftKeyboardTextBoxGotFocus(Object sender, RoutedEventArgs e)

        {

            \\TODO

        }



        private void SoftKeyboardTextBoxLostFocus(Object sender, RoutedEventArgs e)

        {

           \\TODO

        }    

最后,在xaml页面将textbox的style设为上面的值即可;

 

2、重写控件

class VKTextBox : TextBox

    {

        protected override void OnInitialized(EventArgs e)

        {

            base.OnInitialized(e);            

        }



        protected override void OnGotFocus(RoutedEventArgs e)

        {

            base.OnGotFocus(e);

            \\TODO

        }



        protected override void OnLostFocus(RoutedEventArgs e)

        {

            base.OnLostFocus(e);

            \\TODO

        }

    }

将页面的TextBox置换成VKTextBox即可。

 

你可能感兴趣的:(trigger)