locked
How di i select a default item in a combobox RRS feed

  • Question

  • could someone show me how i can select one of my items in my combobox. here is the way i populate it,

    the countries and states are read from a xml file.

    here is the combox in the XAML 

    <ComboBox x:Name="Country"  DisplayMemberPath = "Name" Width="150" HorizontalAlignment="Left"  />


    <ComboBox x:Name="State" ItemsSource="{Binding SelectedItem.States, ElementName=Country}" DisplayMemberPath="Name"   Width="200" HorizontalAlignment="Left" />

    here is where i populate thelocations obj <country and states >

    LocationInfo locations = new LocationInfo();
     locations = await locations.initialize("ms-appx:///CountryList.xml");          
     Country.ItemsSource = locations.Countries;

    here is the country and state class

     public class Country
        {
            public string Name { get; set; }
            public List<State> States { get; set; }
        }
    
        public class State
        {
            public string Name { get; set; }
        }

    Thursday, November 7, 2013 2:49 PM

Answers

  • You could use Linq to help with your finding, for instance 
    IEnumerable<Country> selCountry = from scountry in countries1 where scountry.Name == "Name1" select scountry;
    
    Country1.SelectedItem = selCountry.First();
    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 8, 2013 1:26 PM
    Moderator

All replies

  • Hi Avichai-Rebibo,

    Select a default item in combobox? It seems SelectedIndex and SelectedItem or SelectedValue can be the answer for you. In your situation, you bind Country objects to your combobox, you could use combobox.selectedItem to assign a country object to the combobox.

    For instance I create some test data manually and binding them to the combobox as you did:

                State s1 = new State();
                s1.Name = "sad";
                State s2 = new State();
                s2.Name = "sad1";
                State s3 = new State();
                s3.Name = "sad1";
    
                List<State> states1 = new List<State>();
                states1.Add(s1);
                states1.Add(s2);
                states1.Add(s3);
    
                Country c1 = new Country();
                c1.Name = "Name1";
                c1.States = states1;
    
                List<Country> countries1 = new List<Country>();
                countries1.Add(c1);
    
                Country1.ItemsSource = countries1;
    
                Country1.SelectedItem = c1;

    Hope this will help you.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 8, 2013 1:19 AM
    Moderator
  • Hello Avichai-Rebibo, 

    Jamles is right, you can select first item by 

    Country1.SelectedItem = c1;

    or using selectedIndex.

    Country1.SelectedIndex = 0;


    Friday, November 8, 2013 5:07 AM
  • In your xaml you can simply supply the combobox with SelectedIndex this

    this property will set the default item based on index

    Friday, November 8, 2013 8:11 AM
  • What do I do in my case. how can I set a selected item in the combobox. giving that I set the itemsource the way I do in the code I posted above. ?

     LocationInfo locations = new LocationInfo();
     locations = await locations.initialize("ms-appx:///CountryList.xml");
     Country.ItemsSource = locations.Countries;

    should be something like

    Country.Selecteditem = locations.Countries.Find("United States");

    please help

    here is the initialize class where I deserialize the xml with the coutries and states

     public class LocationInfo 
        {
    
                   
            public List<Country> Countries
            {
                get;
                set;
            }
    
            public async Task<LocationInfo> initialize(string filename)
            {
                var statesFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(filename));
    
                //Deserialize xml data to object
                XmlSerializer xs = new XmlSerializer(typeof(LocationInfo));
                using (Stream stream = await statesFile.OpenStreamForReadAsync())
                {
                    return xs.Deserialize(stream) as LocationInfo;
                }
                
            }
        }

    Friday, November 8, 2013 12:49 PM
  • You could use Linq to help with your finding, for instance 
    IEnumerable<Country> selCountry = from scountry in countries1 where scountry.Name == "Name1" select scountry;
    
    Country1.SelectedItem = selCountry.First();
    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 8, 2013 1:26 PM
    Moderator
  • I think I am into something but my grasp of objects and classes is not very good so here is what I did, but I still cannot see the country I set in the combobox.

     LocationInfo locations = new LocationInfo();
                locations = await locations.initialize("ms-appx:///CountryList.xml");
                Country.ItemsSource = locations.Countries;
                Country.SelectedItem = locations.Countries[1].Name;

    Friday, November 8, 2013 1:52 PM
  • This solution works great. thank you
    Friday, November 8, 2013 1:57 PM
  • Thank you for the response Phumum1-MCTS

    Not really sure this will work since the Combobox is populated with an object of items. I will try that later on when i get home.

    Thursday, November 14, 2013 11:59 AM