BindingContext is always null

Hi,

I am trying to bind the autocomplete control to a ViewModel so that i can use the DisplayMemberPath and SelectedValuePath.

But the value of the BindingContext is always null. Please let me know the reason why it returns an empty value. I am retrieving the values from a SQLite Table.

Code is

In the Page1.xaml

<local:AutoComplete  x:Name="txtStreet" Text="" Watermark="Select" Grid.Column="1" Grid.Row="4" ShowSuggestionsOnFocus="True" DataSource="{Binding DropDownDataCollection}" SelectionChanged="txt_Street_Completed" />

In Page1.xaml.cs

protected override async void OnAppearing()
        {
            clsGlobalData.charDefault = 'A';
            clsGlobalData.strTableNameVM = "M";
           
            txtStreet.BindingContext = new DropDownDataViewModel();
            txtStreet.DisplayMemberPath = "Data";
            txtStreet.SelectedValuePath = "Codevalue";
        }

Code for the ViewModel

public class DropDownDataViewModel
    {

        private ObservableCollection<HH_DropDownData> DropDownDataCollection;

        public ObservableCollection<HH_DropDownData> DropDownDataCollection
        {
            get { return DropDownDataCollection; }
            set { DropDownDataCollection = value; }
        }

        List<HH_DropDownData> RetrieveAllDropDownData = new List<HH_DropDownData>();

        public async Task<List<HH_DropDownData>> PullDataFromDB()
        {

            if (clsGlobalData.charDefault == 'A')
            {
                switch (clsGlobalData.strTableNameVM)
                {
                    // Main Menu
                    case "M":
                        RetrieveAllDropDownData = await App.ExpenseRepo.GetAllCategoryAsync("M");
                        break;
                }
            }

            return RetrieveAllDropDownData;

        }

        public DropDownDataViewModel()
        {
            PullDataFromDB();

            foreach (var item in RetrieveAllDropDownData)
            {
               DropDownDataCollection.Add(item);
            }

        }

    }

Please help how to make the  BindingContext work, since I need to bind it to multiple columns. a list<> does not help in this case. When using async/await functions in the constructor of the ViewModel , it gives unpredictable results.

Thanks,

Rajesh


3 Replies

SK Selva Kumar  Veerakrishnan Syncfusion Team November 23, 2017 12:22 PM UTC

Hi Rajesh,

Thank you for contacting Syncfusion Support.

We have prepared a sample based on your requirement, in which we have used our SfAutoComplete to search the series of data Name. Please download the sample from the link given below.

Sample link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/SFAutoComplete_6268901103.zip  

Please check the sample whether it meets your requirement, if not please update the given below.

1.Sample modification along with the replication procedure.
2.Data(SQLite Table) which has to be added to our SfAutoComplete control.

This could help us to provide better solution on this.

Regards,
Selva Kumar V.


RA Rajesh November 23, 2017 06:00 PM UTC

Hi Selva Kumar,

Thanks a lot for your reply and it helped a lot. But I found that there is a difference in using a loop with dummy data and retrieving data from a SQLite database.

Attaching the entire code for the ViewModel where a query is executed against the SQLite database in an async method.

Also attaching the screen shot of the warning error in Visual Studio since I am unable to use an async/await call for the method that pulls data from the database.

The issue is

When the method PullData in the constructor of the ViewModel executes it has X rows randomly and sometimes it hangs and returns without any data. This is because it is not in an async/await call.

But when it comes to this block of code
foreach (var item in RetrieveAllDropDownData)
                {
                    DropDownDataCollection.Add(item);
                }
the RetrieveAllDropDownData collection is empty and hence nothing is displayed in the autocomplete control.

Hope that you can provide some hints how to make it work when calling a Task in an async method.

Thanks,

Attachment: syncfusion_autoComplete_ViewModel_7d972987.rar


SK Selva Kumar  Veerakrishnan Syncfusion Team November 24, 2017 12:54 PM UTC

Hi Rajesh,

A support incident to track the status of this query has been created under your account.

Please log on to our support website to check for further updates.

Link: https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents/  

Regards,

Selva Kumar V.

Loader.
Up arrow icon