locked
class type confusion please help RRS feed

  • Question

  • Hello,

    I got some confusion with a combobox  that I populate with a class of type LocationInfo "it is a class that contain country and states"

    Then I am trying to select an item from this combobox meaning there is a setting file that populate a class type DefaultCountry "That a default country".

    for some reason the selected item does not appear in the combobox I think it is because the two classes are different.can someone help ?

    Sunday, December 1, 2013 6:08 PM

Answers

  • Hi Avichai,

    If scountry is a Country type it should be fine, also the CountryList.Items should be List<Country>.

    Can you provide me a demo project for testing? Hardly to say why the system cannot recognize Country type just by code snippet. 

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

    • Marked as answer by Avichai-Rebibo Wednesday, December 25, 2013 3:44 PM
    Monday, December 9, 2013 1:10 AM
    Moderator

All replies

  • Hi Avichai-Rebibo,

    Try to compare the display content between “LocationInfo” and “DefaultCountry” to see if the text are the same, if the strings are the same, set this item as the default selected item.

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

    Monday, December 2, 2013 9:44 AM
    Moderator
  • Hi,

    It seems to be the same, when I add the default country to a simple text box just to see the text it works fine. but I cannot display it in the combobox.

    Thursday, December 5, 2013 1:04 AM
  • Hi Avichai,

    LocationInfo contains country and states meanwhile DefaultCountry should contain a type of country, is that correct?

    And can you provide some testing code for us, hardly to reproduce the situation that you are facing.

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

    Thursday, December 5, 2013 9:11 AM
    Moderator

  • Here is the code behind

    Here is how I populate the Country Combobox with all countries

    public async void InitializeCountry()
    {
        //Here I read the XML file that is the list of 
        //countries and states and put it in a LocationInfo 
        //class
        LocationInfo locations = new LocationInfo();
        locations = await locations.initialize("ms-
               appx:///CountryList.xml");
        //Here I add all the countries to the Combobox
         Country.ItemsSource = locations.Countries;
    }


    the following is the procedure that read a xml file that contain settings.

    public async void ReadTheMainSettingsFile()
    {
        //Here we read the XML file and put the settings in   
        //locations1 of DefaultCountry type
        DefaultCountry locations1 = new DefaultCountry();
                    locations1 = await 
                    MainSettings1.initialize<DefaultCountry>
                    ("mySettings.xml");
       
       //Here I create a List of type DefaultCountry Not sure  
       //I really need that
        List<DefaultCountry> ll = new List<DefaultCountry>();
                   ll.Add(locations1);
       
        //Here I do a Linq search for the country name
        IEnumerable<DefaultCountry> selCountry = from scountry 
                   in ll where scountry.Name == 
                   locations1.Name select scountry;
       
        //Here where it doesn't do anything, it suppose to set
        //the selected item to the country name from the LINQ 
        //query       
       Country.SelectedItem = selCountry.First();
       CountryStatus.Text = selCountry.First().Name.ToString();

    Here is the XAML for the ComboBox

    Straight forward

    <StackPanel Orientation="Vertical"  
        HorizontalAlignment="Left">
        <ComboBox x:Name="Country" DisplayMemberPath="Name"  
           Width="150" HorizontalAlignment="Left" />
         <Frame Background="Red">
             <TextBlock x:Name="CountryStatus" Text="{Binding 
              Txt, Source={StaticResource txtVM}}" 
              HorizontalAlignment="Center" 
              Foreground="#FFE8FF00" FontWeight="Bold"/>
          </Frame>
    </StackPanel>

    Here are the  classes used

    This classes are the one that will contain all the countries and states

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

    This ones contain some settings that include a default countrie that I try to set as default to my Combobox

    I call it Name

    public class DefaultCountry 
        {
            public string Name
            {
                get ;
                set ;
            }
            public string City
            {
                get;
                set;
            }
            public string State
            {
                get;
                set;
            }
            public bool SearchHistory
            {
                get;
                set;
            }
            public bool Temprature
            {
                get;
                set;
            }
            public bool DistanceUnit
            {
                get;
                set;
            }
            public MapType CurrentView
            {
                get;
                set;
            }
            public bool ShowZoomControls
            {
                get;
                set;
            }
        }

    I hope this has sense lol

    Thank you


    Friday, December 6, 2013 12:15 AM
  • Hi Avichai,

    I will help to modify the ReadTheMainSettingsFile() method to make your app work.

    I'm not sure why you need do this, you already have your location and why you need new a List contain the location1, the purpose of LINQ is to help to find a Country that have the same name with your DefaultCountry

       //Here I create a List of type DefaultCountry Not sure  
       //I really need that
        List<DefaultCountry> ll = new List<DefaultCountry>();
                   ll.Add(locations1);
       
        //Here I do a Linq search for the country name
        IEnumerable<DefaultCountry> selCountry = from scountry 
                   in ll where scountry.Name == 
                   locations1.Name select scountry;

    I will suggest you code like below, but the code may not work, because your combobox is named Country, which may confuse the system which Country I'm referring to, a Class or an Item?

    IEnumerable<Country> selCountry = from scountry in Country where scountry.Name == locations1.Name select scountry;
    
    Country.SelectedItem = selCountry.First();

    If you modify the combobox name as "CountryList" and also import the Contry class in your code, you could use following code to make it works fine:

    //Country is your class
    //CountryList is your combobox
    IEnumerable<Country> selCountry = from scountry in CountryList where scountry.Name == locations1.Name select scountry;
    
    CountryList.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, December 6, 2013 6:53 AM
    Moderator
  • Thank you

    I renamed the combobox to CountryList and changed the LINQ statement.

    I get this error on the LINQ statetement

    Friday, December 6, 2013 1:01 PM
  • Oops, should be CountryList.Items.

    --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, December 6, 2013 1:58 PM
    Moderator
  • now  the scountry.Name is underligned with red error

    Friday, December 6, 2013 11:32 PM
  • Hi Avichai,

    If scountry is a Country type it should be fine, also the CountryList.Items should be List<Country>.

    Can you provide me a demo project for testing? Hardly to say why the system cannot recognize Country type just by code snippet. 

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

    • Marked as answer by Avichai-Rebibo Wednesday, December 25, 2013 3:44 PM
    Monday, December 9, 2013 1:10 AM
    Moderator
  • I can certainly do provide a test project. I will post it later on after work today.
    Monday, December 9, 2013 12:08 PM
  • Here solved, you were right just some type confusion.

     IEnumerable<Country> selCountry = from scountry in locations.Countries where scountry.Name == locations1.Name select scountry;
    CountryList.SelectedItem = selCountry.First();

    Wednesday, December 25, 2013 3:44 PM