xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Binding with UpdateSourceEventName doesn't work (tested on Entry and Editor) #9704

Open nk54 opened 4 years ago

nk54 commented 4 years ago

Description

I have an Entry and an Editor. Their Text property is binded to my ViewModel. I would like to raise the binding when the user finishes/completes its input. By default, if a user types "blabla", the binding is raised for each character (6 times). I want to raise the binding when the control gets "Unfocused" or "Completed" (2 events available on those controls). Thus, the binding will only be raised when the typing is finished.

For this purpose, I have tried to use either Text="{Binding MyText, UpdateSourceEventName=Completed}" or Text="{Binding Text, UpdateSourceEventName=Unfocused}"

But they both don't work.

Steps to Reproduce

  1. Add an Entry or an Editor control
  2. Create a string property with INPC in the ViewModel
  3. Bind the Text property of the Entry/Editor
  4. Add UpdateSourceEventName=Unfocused (or Completed) in the binding. You must have something similar to Text="{Binding TheStringProperty, UpdateSourceEventName=Unfocused}" (or Completed)

Expected Behavior

The binding is raised only once : when either the control is unfocused or the inputs is validated/completed.

Actual Behavior

The binding is raised on each key touched instead of when the input is complete.

Workaround

Instead of using a binding to the string property, use a CommandBehavior and bind to the Unfocused event to the Command. In the command, you set the string property with the text provided thanks to a binding : Parameter to the Text property of the control (with x:Reference)

Exemple given :

<Entry x:Name="TheEntry" Text="{Binding SomeTextOfViewModel, Mode=OneWay}">
    <Entry.Behaviors>
        <b:EventToCommandBehavior
            Command="{Binding EditCommand}"
            CommandParameter="{Binding Source={x:Reference TheEntry}, Path=Text}"
            EventName="Unfocused" />
    </Entry.Behaviors>
</Entry>
jsuarezruiz commented 4 years ago

I attach a repro sample: Issue9704.zip