Two comboboxes, OnLoaded Event?

I have two combo boxes, one for Country and one for State. I can successfully add the U.S. State names when the Country combobox is selected to United States using the OnSelectionChanged event. 


        private void CountryPicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e)

        {


            if (CountryPicker.SelectedValue != null && CountryPicker.SelectedValue.ToString() == "United States")

            {

                StatePicker.DataSource = UsStates;

                StatePicker.DisplayMemberPath = "State";

                StatePicker.IsEditableMode = false;


            }

            else

            {

                StatePicker.DataSource = null;

                StatePicker.IsEditableMode = true;

            }

        }



However, when I start the page and the Country combobox already shows United States (default), I can't figure out how to load the States into the State combobox. Page OnAppearing does not seem to work since it looks like the combobox hasn't loaded yet. Any suggestions?

11 Replies

RS Ruba Shanmugam Syncfusion Team May 1, 2020 09:56 AM UTC

Hi Reza Mohamed,

Greetings from Syncfusion.

We are validating your requirement and we have prepared a sample based on your requirement. When selecting the country ComboBox, the value of state ComboBox is changed.

When setting the ComboBox SelectedIndex at sample, it initially calls the SelectionChanged event from which you can set the value.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/MultipleCombobox1267939052  

Code Snippet:
 
XAML: 
 <StackLayout>

<Label Text="Country"/>

<comboBox:SfComboBox x:Name="countryComboBox"

MaximumDropDownHeight="200"

SelectedIndex="0"

DataSource="{Binding CountryList}"

SelectionChanged="country_SelectionChanged"/>

<Label Text="States"/>

<comboBox:SfComboBox x:Name="stateComboBox"

MaximumDropDownHeight="200"

DataSource="{Binding StateList}"/>

</StackLayout>


XAML.cs:

private void country_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)

{

viewModel.StateList.Clear();

if (e.Value.ToString() == "India")

{

viewModel.StateList.Add("Tamilnadu");

viewModel.StateList.Add("Kerala");

viewModel.StateList.Add("Maharatra");

viewModel.StateList.Add("Karnataka");

viewModel.StateList.Add("Punjab");

}
}
 
 

We ask you to try our sample, if this is different from your requirement, you can return us with more information, please. This will help us to further investigate and provide you with a better solution at the earliest possible time.


Regards,

Ruba Shanmugam
 
 



RE Reza May 2, 2020 03:58 AM UTC

Can you check the attached example, please? I modified your code to match what I have closely. 

The code works fine on Android, but not on iOS - that is the issue.

Attachment: MultipleCombobox_with_updates_74343523.zip


RS Ramya Soundar Rajan Syncfusion Team May 4, 2020 11:36 AM UTC

Hi Reza, 
 
We have analyzed your query and modified the sample based on the below code snippet to resolve the reported issue. Please find the below code snippet and sample for this. 
 
Code Snippet: 
 private void CreateLists() 
        { 
            CountriesCollection = new ObservableCollection<Country> 
            { 
                new Country {Key = "usa", Value = "United States"}, 
                new Country {Key = "ind", Value = "India"}, 
                new Country {Key = "arg", Value = "Argentina"}, 
                new Country {Key = "rus", Value = "Russia"} 
            }; 
 
            SelectedCountry = CountriesCollection[0]; 
 
        } 

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/MultipleCombobox1267939052_2-2078932835

Please check with the above and let us know if you have any concern. 
 
Regards, 
Ramya S 



RE Reza May 5, 2020 01:20 AM UTC

You are changing my use case from being able to set the SelectedItem with text to an index, that is not acceptable. If it works in one platform then my expectation is that it should work on the other, if not this is a bug. 

I don't know the index beforehand, only the key value of the SelectedCountry. If I was to use the index, then I have to do an extra iteration.
var z = new Country { Key = "usa", Value = "United States" };
var index = CountriesCollection.IndexOf(p => p.Key == z.Key);
 
SelectedCountry = CountriesCollection[index];

But again if it works in Android with just setting the SelectedCountry directly, why doesn't it work in iOS?





SP Sakthivel Palaniyappan Syncfusion Team May 5, 2020 01:48 PM UTC

Hi Reza,

We confirmed this as bug and we have logged a defect report regarding on this and it can be tracked through our feedback portal below.

Feedback link: https://www.syncfusion.com/feedback/14104/selectionchanged-event-not-fired-in-ios

We will provide patch on May 22nd , 2020. We appreciate your patience until then.

Regards,
Sakthivel P.



RE Reza May 6, 2020 06:40 AM UTC

Once I have the United States and a US state -Arizona chosen, When I change the country dropdown to another country, I want to reset the States comboxbox from the chosen US state to be blank. However, the below code doesn't seem to reset the combobox. What am I doing wrong?

private void country_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)
{
 
    //if (e.Value.ToString() == "United States")
    if (CountryPicker.SelectedValue.ToString() == "United States")
    {
        StatePicker.DataSource = UsStates;
        StatePicker.DisplayMemberPath = "State";
        StatePicker.IsEditableMode = false;
    }
    else
    {
        StatePicker.SelectedValue = null;
        StatePicker.DataSource = null;
        StatePicker.IsEditableMode = true;
    }

Attachment: MultipleCombobox_reset_states_ac433eba.zip


SP Sakthivel Palaniyappan Syncfusion Team May 7, 2020 10:06 AM UTC

Hi Reza,

We would like to inform that the you can achieve your requirement by setting  SelectedItem  as Null as like below code snippet.

Code Snippet:

 
 if (CountryPicker.SelectedValue.ToString() == "United States") 
            { 
                StatePicker.DataSource = UsStates; 
                StatePicker.DisplayMemberPath = "State"; 
                StatePicker.IsEditableMode = false; 
            } 
            else 
            { 
                StatePicker.SelectedItem = null; 
                StatePicker.SelectedValue = null; 
                StatePicker.DataSource = null; 
                StatePicker.IsEditableMode = true; 
            } 

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/MultipleCombobox1267939052_31961541778

Please check with the above sample and let us know if you have any concern.

Regards,
Sakthivel P.
 



RE Reza May 7, 2020 08:53 PM UTC

Setting SelectedItem=null did not work in my main project, it worked in the smaller demo project. 

For reference, I have updated the issue tracker with reference to my real project so you can diagnose and tell me what's going on, please.

https://www.syncfusion.com/feedback/14104/selectionchanged-event-not-fired-in-ios


SP Sakthivel Palaniyappan Syncfusion Team May 8, 2020 08:15 AM UTC

Hi Reza,

We have checked the reported problem in provided project and it does not set the SelectedValue to Null. Hence we have modified the BindingMode as TwoWay in sample to achieve your requirement.  Please find the below code snippet for this,

Code Snippet:

 
 SelectedValue="{Binding State, Mode=TwoWay}" 

Please check with the above code snippet in your sample for the SelectedPicker and let us know if the issue is resolved or not  from your side.

Regards,
Sakthivel P.



RE Reza May 8, 2020 09:17 PM UTC

(1) How is it that the identical code works in your sample but not on mine? Without setting the {bindingmode}

(2) I also noticed that in your project I am able to set

CountriesCollection.IndexOf(p => p.Value == z);


but theoretically, there is no base class library for IndexOf that takes a Func<T, bool> . I was actually not aware of that when I tried it on your project as above and it worked. But when I tried it on my project it throws an error, which is why I did some research.




JK Jeya Kasipandi Syncfusion Team May 11, 2020 11:45 AM UTC

Hi Reza, 
 
Query 1: How is it that the identical code works in your sample but not on mine? Without setting the {bindingmode} 
 
We suspect that the Text is also bound in your project might be the case. 
 
Query 2: IndexOf that takes a Func<T, bool> 
  
We suspect the namespace "using Xamarin.Forms.Internals"  missed in the project which might be the cause for the issue. 

Please check the same and let us know if you need further assistance.
 
 
Regards, 
Jeya K 


Loader.
Up arrow icon