2010-03-10 8 views
0

두 개의 콤보 상자가있는 양식을 만들어야합니다. 국가를 선택하면 그 국가의 도시를 얻습니다.DataBinding 2 콤보 상자 wpf

나는 wpf에 처음 왔기 때문에 내가 무엇을 놓치고 있는지 잘 모르겠다. 현재로서는 그것을 채우지 않습니다.

정말 도움이되는 도움말이 있습니다.

내가 무엇을했는지 있습니다 :

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

모델

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에 뷰의 데이터 컨텍스트를 설정하는 엘 어딘가? 나는 위에 열거 된 코드에서 그것을 보지 못했다.

예컨대

var viewModel = new CountryCityViewModel (countryCityModel, repository);

window.DataContext = viewModel;

+0

안녕하세요, 내 게시물을 수정했으며보기 생성자를 놓쳤습니다. 코드를 추가했지만 지금은 wpfDataBinding.Types.Country가 있고 국가 이름이 아닙니다. 어떻게 도시를 채울 수 있습니까? – user9969

+0

combobox의 DisplayMemberPath는 바인딩식이 아닌 구성원의 이름이어야합니다. 예 : DisplayMemberPath = "이름" –