2010-03-10 83 views
0

我需要建立一個表單,我有2個組合框。 選擇國家,你會得到那個國家的城市。DataBinding 2 ComboBoxes wpf

我新來wpf所以幫助我,因爲我不知道我失蹤。 目前它甚至不填充它。

任何幫助建議真的appreaciated!

這是我做了什麼:

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(); 
     } 
    } 

主窗口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; 
    } 
} 

型號

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; } 
} 

類型

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

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

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

你視圖的數據上下文設置爲您ViewMod el某處?我在上面列出的代碼中沒有看到。

例如

var viewModel = new CountryCityViewModel(countryCitymodel,repository);

window.DataContext = viewModel;

+0

嗨, 我編輯了我的文章,我錯過了視圖構造函數。 我已經添加了你的代碼,但現在我得到了wpfDataBinding.Types.Country,而不是國家的名稱 我怎樣才能填充城市? – user9969 2010-03-10 20:51:45

+0

組合框上的DisplayMemberPath應該是成員的名稱,而不是綁定表達式。即 DisplayMemberPath =「Name」 – 2010-03-10 21:28:16