tags:

views:

134

answers:

1

Hi 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  A: 

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;

Kevin Dente
Hi,I have edited my post and I missed the View Constructors .I have added your code but now I get wpfDataBinding.Types.Country and not the names of the countriesAlso how I can then populate the cities?
DisplayMemberPath on the combobox should be the name of the member, not a binding expression. ie DisplayMemberPath="Name"
Kevin Dente