Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:20:23+00:00 2026-05-13T23:20:23+00:00

I need to build a form where I have 2 comboBoxes . Select country

  • 0

I need to build a form where I have 2 comboBoxes .
Select country and you get the cities of that country.

I m new to wpf so help me as I not sure what I am missing.
At the moment It doesnt even populate it.

Any help suggestions really appreaciated!

This is what I have done:

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
           base.OnStartup(e);

           var window = new MainWindow();
           var countryCitymodel = new CountryCityModel();
          var repository = new CountryCityRepository();
          var viewModel = new CountryCityViewModel(countryCitymodel, repository);
          window.Show();
        }
   }

MainWindow xaml

      <Window x:Class="WpfDatabinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:view="clr-namespace:WpfDatabinding.Views"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <view:CountryCityView />
        </Grid>
</Window>

CountryCityView xaml

        <UserControl x:Class="WpfDatabinding.Views.CountryCityView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="75" d:DesignWidth="300">

       <Grid Height="64" Width="291">
       <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
       <Grid.ColumnDefinitions>
        <ColumnDefinition Width="97"/>
        <ColumnDefinition Width="13" />
        <ColumnDefinition Width="*"/>
       </Grid.ColumnDefinitions>
    <Label  Content="Countries" Margin="-6,6,5,0" Grid.ColumnSpan="2" Height="33"></Label>
    <Label Grid.Row="1" Content="Cities" Grid.ColumnSpan="2"></Label>
    <ComboBox Name="cboCountries" 
              ItemsSource="{Binding Path=Countries}" 
              SelectedValuePath="Name"
              DisplayMemberPath="{Binding Name}" 
              Grid.Column="2" 
              Margin="0,10"></ComboBox>
    <ComboBox Name="cboCities" 
              Grid.Column="2" 
              Grid.Row="1" 
              ItemsSource="{Binding Path=Cities}" Height="20" Margin="0,0,0,1">            
            </ComboBox>
        </Grid>
</UserControl>

CountryCityView

   public partial class CountyrCityView:UserControl
   { 
     public CountryCityView()
     {
         InitializeComponents();

      }
     public CountryCityView(CountryCityViewModel countryCityViewModel)
     {
         InitializeComponents();
         DataContext=countryCityViewModel;

      }
   }

CountryCityViewModel

public class CountryCityViewModel : ViewModelBase
    {
        private readonly CountryCityModel _countryCityModel;
        readonly CountryCityRepository _repository;
        RelayCommand _getCountriesCommand;
        private RelayCommand _getCitiesCommand;

        public CountryCityViewModel(CountryCityModel countryCityModel, CountryCityRepository repository)
        {
                _countryCityModel = countryCityModel;
                _repository = repository;
            GetCountries.Execute(null);
        }

    public List<Country> Countries
    {
        get { return _countryCityModel.Countries; }
        set
        {
            _countryCityModel.Countries = value;
            OnPropertyChanged("Countries");
        }
    }

    public List<City> Cities
    {
        get { return _countryCityModel.Cities; }
        set
        {
            _countryCityModel.Cities = value;
            OnPropertyChanged("Cities");
        }
    }

    public Country SelectedCountry
    {
        get { return _countryCityModel.SelectedCountry; }
        set
        {
            _countryCityModel.SelectedCountry = value;
            OnPropertyChanged("SelectedCountry");
        }
    }

    public City SelectedCity
    {
        get { return _countryCityModel.SelectedCity; }
        set
        {
            _countryCityModel.SelectedCity = value;
            OnPropertyChanged("SelectedCity");
        }
    }

    public ICommand GetCountries
    {
        get
        {
            if (_getCountriesCommand == null)
            {
                _getCountriesCommand = new RelayCommand(param => GetCountryList(), param => CanGetCountries());
            }
            return _getCountriesCommand;
        }
    }
    public ICommand GetCities
    {
        get
        {
            if (_getCitiesCommand == null)
            {
                _getCitiesCommand = new RelayCommand(param => GetCityList(), param => CanGetCities());
            }
            return _getCitiesCommand;
        }
    }
    private List<Country> GetCountryList()
    {
        Countries = _repository.GetCountries();
        return Countries;
    }
    private static bool CanGetCountries()
    {
        return true;
    }
    private List<City> GetCityList()
    {
        Cities = _repository.GetCities(SelectedCountry.Name);
        return Cities;
    }
    private static bool CanGetCities()
    {
        return true;
    }
}

Model

public class CountryCityModel
{
    public List<Country> Countries { get; set; }

    public List<City> Cities { get; set; }

    public Country SelectedCountry{ get; set; }
    public City SelectedCity { get; set; }
}

Types

public class City
    {
        public string Name { get; set; }
        public string CountryName { get; set; }
    }

public class Country
    {
        public string Name { get; set; }
    }

Repository

    public List<Country>GetCountries()
    {
        return new List<Country>
                   {
                       new Country{Name = "Italy"},
                       new Country{Name = "Germany"},
                       new Country{Name = "France"},
                       new Country{Name = "England"}
                   };
    }
    public List<City> GetCities(string countryName)
    {
        return Cities().Where(c => c.CountryName == countryName).ToList();
    }

    private static IEnumerable<City> Cities()
    {
        return new List<City>
                   {
                       new City { CountryName="Italy",Name = "Rome"},
                       new City {CountryName="France",Name = "Paris"},
                       new City{CountryName="Germany",Name ="Berlin"},
                       new City{CountryName="England",Name ="London"}
                   };
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:20:23+00:00Added an answer on May 13, 2026 at 11:20 pm

    Are you setting the data context of the view to your ViewModel somewhere? I don’t see that in the code listed above.

    e.g.

    var viewModel = new CountryCityViewModel(countryCitymodel, repository);

    window.DataContext = viewModel;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using regular asp.net c# (webforms) i need to build a simple form that
within a form I have a section that allows the user to build out
I need to build a form that allows a user to enter 100 different
I'm trying to use JSTL to build a form. I have a select input
Need: Ability to dynamically build Forms Structure (simple idea not actual structure) Admin: Form
I need to build a dynamic form from database. I have following Entity to
I need to build a class in this form namespace BestCompanyEver { public class
I'm in need of a dynamic form that will be built from a list
I hope someone can help me..... i am trying to build a dynamic form
I have a page I need to build out where depending on the selection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.