Override of OnGotFocus does not work

Please report bugs here. If you are unsure whether something is a bug or an expected behavior, please post it on the "Technical Support" forum instead, and wait for a moderator to handle/move the post.
xwang
Posts: 4
Joined: Mon Sep 05, 2016 5:46 pm

Override of OnGotFocus does not work

Postby xwang » Tue Jan 17, 2017 10:35 pm

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

JS-Support
Site Admin
Posts: 813
Joined: Tue Apr 08, 2014 3:42 pm

Re: Override of OnGotFocus does not work

Postby JS-Support » Wed Jan 18, 2017 7:57 am

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

Henrygo
Posts: 8
Joined: Thu Nov 24, 2016 10:42 pm

Re: Override of OnGotFocus does not work

Postby Henrygo » Wed Jan 18, 2017 5:19 pm

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?

JS-Support
Site Admin
Posts: 813
Joined: Tue Apr 08, 2014 3:42 pm

Re: Override of OnGotFocus does not work

Postby JS-Support » Sun Jan 29, 2017 4:32 am

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


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 2 guests

 

 

cron