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. Image for the cookie policy date

Binding SelectedValue MVVM

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?


8 Replies

PK Pavendhan Kumar Syncfusion Team 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. 



AN Andreas 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.


DR Dhanasekar R Syncfusion Team 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 



AN Andreas 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


AN Andreas 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?


DR Dhanasekar R Syncfusion Team 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 



AN Andreas 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


DR Dhanasekar R Syncfusion Team 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 


Loader.
Up arrow icon