locked
TextBox Lost focus while user type RRS feed

  • Question

  • Hello Friends

    I am working on one type of calculator project. In that I am using Mahapps Metro control. Its working fine but some time when user type on textbox or select on combobox  project lost  focus on particular control. I couldn't able to find the reason. I just want to know Why control loosing there focus ? How I can overcome this problem.  Why this problem occurring? etc.

    I didnt implemented selection change event on that textbox, just simple text binding which holds string property, and for combox I ave bind collection property and selectedItem property, Combobox implements selection change event. 

    My both controls (textbox and Combobox) loose there focus some time. 

     
    Wednesday, May 4, 2016 7:11 AM

Answers

  • Mahapps metro just styles controls.

    I'm pretty sure it doesn't add any setting of focus to some other control when you type in a textbox.

    It seems rather unlikely that Mahapps itself is doing this.

    Much more likely there is some handler you've added, Gopal.

    If you create a new window in your app with two textboxes in it. ( Stick em in a stackpanel ).

    No extra event handlers etc.

    Does the first one lose focus as you type?

    If it does then do you have any implicit style targets textbox?

    And what is that?

    .

    Incidentally you say:

    Combobox implements selection change event. 

    Since you're binding anyhow, I suggest you take a look at MVVM.

    Rather than a selection change event you'd bind selected item like this:

    http://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx#Select_From_List_IndexChanged

    The sort of property you'd have in your viewmodel:

    private Person selectedChef;
    
    public Person SelectedChef
    {
        get
        {
            return selectedChef;
        }
        set
        {
            selectedChef = value;
            RaisePropertyChanged();
            DoSomethingWhenChefChanged();
        }
    }

    This also obviates one of the oddities of WPF comboboxes.

    When you handle index or selection changed, it doesn't give you the new thing you selected because it's not selected at that point. You need to defer the code you'd just put in a winforms style handler.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Gopal Mahale Wednesday, May 18, 2016 6:33 AM
    Friday, May 6, 2016 8:23 AM

All replies

  • Hi

    I am using mahapps controls in our WPF project. We have few textbox controls which gets unfocused automatically while typing into textbox.

    Also sometimes combobox and other controls are not getting selected in first click.

    Please suggest.

    Thanks in advance.



    Best Regards, Somnath


    • Edited by Somnath Tingare Wednesday, May 4, 2016 6:58 AM
    • Merged by Andy ONeill Wednesday, May 4, 2016 8:07 AM duplicate
    • Marked as answer by Gopal Mahale Wednesday, May 18, 2016 6:33 AM
    • Unmarked as answer by Gopal Mahale Wednesday, May 18, 2016 6:33 AM
    Wednesday, May 4, 2016 6:58 AM
  • This isn't normal behaviour.

    You have code or something which takes focus from the control or raises property changed so the control gets the bound value again... or something else.

    If you want more specific help you will need to explain in more detail what your solution is doing.

    I strongly recommend reproducing this issue in a small solution which has no external dependencies.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, May 4, 2016 7:55 AM
  • If you issue is related to Mahapps.Metro you should report it/ask at GitHub: https://github.com/MahApps/MahApps.Metro/issues.

    These forums are intended for Microsoft's products and technologies only.

    If not, you should provide a full but minimal sample of your issue that can easily be reproduced by someone else. You could for example upload a sample project to OneDrive and post the link to it here.

    If you don't manage to reproduce your issue in a blank WPF application without Mahapps.Metro, it is probably not a WPF issue but a Mahapps.Metro related issue and then you should ask a GitHub and not here.

    Thanks for your understanding.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    Thursday, May 5, 2016 7:17 PM
  • Mahapps metro just styles controls.

    I'm pretty sure it doesn't add any setting of focus to some other control when you type in a textbox.

    It seems rather unlikely that Mahapps itself is doing this.

    Much more likely there is some handler you've added, Gopal.

    If you create a new window in your app with two textboxes in it. ( Stick em in a stackpanel ).

    No extra event handlers etc.

    Does the first one lose focus as you type?

    If it does then do you have any implicit style targets textbox?

    And what is that?

    .

    Incidentally you say:

    Combobox implements selection change event. 

    Since you're binding anyhow, I suggest you take a look at MVVM.

    Rather than a selection change event you'd bind selected item like this:

    http://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx#Select_From_List_IndexChanged

    The sort of property you'd have in your viewmodel:

    private Person selectedChef;
    
    public Person SelectedChef
    {
        get
        {
            return selectedChef;
        }
        set
        {
            selectedChef = value;
            RaisePropertyChanged();
            DoSomethingWhenChefChanged();
        }
    }

    This also obviates one of the oddities of WPF comboboxes.

    When you handle index or selection changed, it doesn't give you the new thing you selected because it's not selected at that point. You need to defer the code you'd just put in a winforms style handler.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Gopal Mahale Wednesday, May 18, 2016 6:33 AM
    Friday, May 6, 2016 8:23 AM