We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Binding SelectedValue MVVM

Thread ID:

Created:

Updated:

Platform:

Replies:

132298 Aug 24,2017 03:18 PM UTC Dec 26,2018 10:07 AM UTC Xamarin.Forms 8
loading
Tags: SfAutoComplete
Ian Nájar
Asked On August 24, 2017 03:18 PM UTC

Hi,

I would like to know how to bind the selectedvaue to a viewmodel property. To me is me very easy in other controls, however I have some difficulties with your control because it doesn't work as I would like.

In my viewmodel, I have a property called "SelectedCustomer" which is a type "Customer" class.
The only way to update this property, is from view by creating a method in code-behind to SelectionChanged, and then assing this value directly, which clearly breaks MVVM pattern.


void txtCustomer_SelectionChanged(object sender, Syncfusion.SfAutoComplete.XForms.SelectionChangedEventArgs e)
{
     vm.SelectedCustomer = !String.IsNullOrEmpry(e.Value.ToString()) ? (Customer)e.Value : null;
}

The only way to get the entire object and not only the string of the selectedcustomer is in e.Value;
Is there any other alternative to bind with a more MVVM friendly approach?


Pavendhan Kumar [Syncfusion]
Replied On August 25, 2017 08:11 AM UTC

Hi Ian,

Thank you for using Syncfusion products.

Currently we do not have any direct property to access the selected item from AutoComplete other than SelectionChanged event. We have considered your requirement as feature request and logged a feature report for the same “Support to access a SelectedItem from AutoComplete”. The feature will be implemented and included in any of our upcoming release.

Regards,
Pavendhan K. 


Andreas
Replied On December 20, 2018 02:01 PM UTC

Hello. Thanks for the quick and the best support for the issues.

I would like to continue the thread. Using sfAutocomplete 16.4.0.42 biding assigning the relevant property to the SelectedValue property works only first time. SelectedItem property becomes an empty string after the erasing . As a workaround we r using SelectedIndex bindable property, but this way we could not fill the text value by property changed mechanism - visible control text has been never updated.

Also after filling the text and selected values manually we detect that it could not update bindable properties in TwoWay manner, erasing the text by the user. That scenario works when we choose the value from the control's dropdown without any problems.

Thanks.

Dhanasekar R [Syncfusion]
Replied On December 21, 2018 12:44 PM UTC

Hi Andreas,

 
Thanks for your update. 
 
Your requirement can be achieved by binding the values to the SelectedItem property. We have prepared the sample based on your requirement in which we have bind the SelectedItem property. Please have the sample from the below link. 
 
 
Please check our sample and let us know if any further clarifications on this. 
 
Regards, 
Dhanasekar 


Andreas
Replied On December 23, 2018 06:16 PM UTC

Hello. Thanks for the quick feedback. I have checked the solution attached and still have some issues. Please check my xamarin solution as well.

1. Initial assignment of SelectedItem in viewmodel constructor works OK. But if we fill selected item by pressing the button(SelectValueCommand invoke) its not affect the view - will implement INotifyPropertyChanged and update this thread

Looks like its working fine using the model provided, should it be unique rows? Is ID required? Our models currently are not simple, should we use simplified versions?

2. When the value is selected by the constructor I can not change it to null manyally erasing the text in the view, like its happening after the selection from drop down list.

Thanks.

Attachment: AutoCompleteSample_773bbae5.zip

Andreas
Replied On December 24, 2018 07:17 AM UTC

Finally the issue is the following - erase the item with the cross button or invoking Clear() does not change the selecteditem and as a result the next choosing(if we choose the same item via bindings) does not update the autocomplete view, any suggestions?

Dhanasekar R [Syncfusion]
Replied On December 24, 2018 12:19 PM UTC

Hi Andreas, 
 
Thanks for your update. 
 
We have validated your sample and we found that you have set value for private field (i.e.) selectedviews.  So the property changed will not be invoked while setting private field. Instead you have to set public property (i.e.) SelectedViews as like below code snippet. 
  
 return new Command(() => 
    { 
          SelectedViews = employeeCollection[1]; 
    }); 
 
Please use the above code snippet on button click. We have modified the same sample, please download it from the link given below. 
 
Sample linkSample 
 
Please let us know if you have any clarifications on this. 
 
Regards, 
Dhanasekar 


Andreas
Replied On December 24, 2018 01:02 PM UTC

Finally the issue is the following - erase the item with the cross button or invoking Clear() does not change the selecteditem and as a result the next choosing(if we choose the same item via bindings) does not update the autocomplete view, any suggestions?

Please use th uploaded solution.

Erase the text value manually and assign the same collection item pressing the button - no action.

Attachment: AutoCompleteSample1730846693_fe015797.zip

Dhanasekar R [Syncfusion]
Replied On December 26, 2018 10:07 AM UTC

Hi Andreas, 
 
Thanks for the additional information 
 
We are able to reproduce this issue "Selected Item is not updating correctly with same item " on Xamarin.Forms Android and iOS platform. The fix for this issue will be included in our 2018 Volume 4 Service pack 1, which is expected to be rolled out by end of January 2019. You can track the status from below link 
 
 
We can overcome this issue by changing the SelectedItem as null in the ValueChanged event as code snippet given below. 
 
autoComplete.ValueChanged += (sender, e) =>
{
    if (e.Value.Length == 0)
    {
       autoComplete.SelectedItem = null;
       autoComplete.IsFocused = true;
     }
}; 

We have prepared a sample for the same, please download the sample from below link. 
 
Sample linkSample 
 
Please let us know if you have any further clarifications on this. 
 
Regards, 
Dhanasekar 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;