Page 1 of 1

Override of OnGotFocus does not work

Posted: Tue Jan 17, 2017 10:35 pm
by xwang
Hello Team,

I create a control custom control CMTextBox which inherits Textbox. And overrode the method 'OnGotFocus'.
But the GotFocus event is not triggered (OnGotFous is not called) when focus is moved to my CMTextBox.
Method 'OnLostFocus' also has the same problem.

However, overridden method OnPointerEntered works well.

Is this a bug of OnGotFocus and OnLostFocus?
If not, how to raises the GotFocus event and make method 'OnGotFocus' work.

Generic.xaml:

Code: Select all

<Style x:Key="CMTextBoxBaseStyle" TargetType="local:CMTextBoxBase">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CMTextBoxBase">
                    <TextBox>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="States">
                                <VisualState x:Name="Normal">
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </TextBox>
                   
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


Override Method:

Code: Select all

   protected override void OnGotFocus(RoutedEventArgs eventArgs)
        {
            base.OnGotFocus(eventArgs);
        }

        protected override void OnPointerEntered(PointerRoutedEventArgs eventArgs)
        {
            base.OnPointerEntered(eventArgs);
        }


Regards,
XWang

Re: Override of OnGotFocus does not work

Posted: Wed Jan 18, 2017 7:57 am
by JS-Support @Userware
Hello XWang,

Thanks for your message.

Your ControlTemplate cannot contain another <TextBox>. If you do that, you will end up with 2 TextBoxes, where one TextBox is contained inside the other.

At the moment you cannot specify a different ControlTemplate for the TextBox because the TextBox is rendered as a native html DOM element (more precisely, it is a DIV with the "contenteditable" attribute). There is no way to customize the look of the built-in TextBox other than by setting its built-in properties (Foreground, Background, FontSize, Padding, etc.) OR by wrapping it inside a UserControl and putting stuff around it.

If you remove the ControlTemplate, overriding the OnGotFocus method should work fine. I have just tested it with the following code:

C#:

Code: Select all

public class TextBox2 : TextBox
{
    protected override void OnGotFocus(RoutedEventArgs eventArgs)
    {
        MessageBox.Show("Overriding OnGotFocus appears to work properly.");

        base.OnGotFocus(eventArgs);
    }
}


XAML:

Code: Select all

<local:TextBox2 Text="This is a test" />


Regards,
JS-Support

Re: Override of OnGotFocus does not work

Posted: Wed Jan 18, 2017 5:19 pm
by Henrygo
Hi team,

The code you provided is running well in the browser, but in the simulator, the event OnGotFocus can not be triggered.
What is the reason?

Re: Override of OnGotFocus does not work

Posted: Sun Jan 29, 2017 4:32 am
by JS-Support @Userware
Hi,

Indeed, there appears to be an issue with OnGotFocus in the Simulator. We are going to investigate. I'll keep you updated.

Regards,
JS-Support