Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Set a boolean property (in ViewModel) to false when a SearchBox looses its focus?

FranciscoGGFranciscoGG ESMember ✭✭
edited August 2015 in Xamarin.Forms

Hi,

I am using MVVM, and I want to put a boolean property (IsSearchBoxFocused) to false, when a SearchBox looses its focus.

Would it be possible to do this with Triggers? How could I do this avoiding Code-Behind or Messenger?

Thanks.

Posts

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    The way I understand MVVM, your ViewModel shouldn't even know there's a SearchBox there.

    I think if you are able to explain what you're trying to do we may be able to uncover a different approach that doesn't involve polluting your ViewModel with View-type information. :smile:

  • FranciscoGGFranciscoGG ESMember ✭✭
    edited August 2015

    @DavidDancy, but which approach would yo do?

    I want to load some data from server (calling a DataService), and then, when the data is loaded, I want to put the focus into the SearchBox...

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    Hmm. To my mind the two things you've mentioned are opposites.

    I want to put a boolean property (IsSearchBoxFocused) to false, when a SearchBox looses its focus

    I want to load some data from server (calling a DataService), and then, when the data is loaded, I want to put the focus into the SearchBox...

    These two things are not the same. In the first case, you're wanting to update a VM property after the user finishes typing in the SearchBar. In the second, you're wanting to jump into the SearchBar after loading some data in the VM.

    So for the first case, the SearchBar inherits the Focused and Unfocused events from VisualElement. You can bind these to commands in your VM and set your property as required.

    For the second case, I think it will be quite a lot easier to make a little bit of code-behind on your page. If you have an event in your VM that gets triggered when the load completes, you can subscribe to the event in your page and call the Focus method of SearchBar to trigger the focus jump.

Sign In or Register to comment.