2012-04-01 7 views
0

XAMLとC#の両方のコードを添付しましたが、動作しません。お知らせ下さい。おかげ2 comboxを互いにやりとりさせる方法RSS

enter code here

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"></ColumnDefinition> 
     <ColumnDefinition Width="*"></ColumnDefinition> 
     <ColumnDefinition Width="4*"></ColumnDefinition> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="0.7*"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="0.7*"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="5*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <TextBlock Text="Select Country" Grid.Row="0" Grid.Column="0"></TextBlock> 
    <ComboBox x:Name="CmbxCountry" SelectionChanged="CmbxCountry_SelectionChanged" Grid.Row="0" Grid.Column="1"></ComboBox> 
    <TextBlock Text="Select State" Grid.Row="2" Grid.Column="0"></TextBlock> 
    <ComboBox x:Name="CmbxState" Grid.Row="2" Grid.Column="1"></ComboBox> 
</Grid> 

名前空間Survey_Model { 公共部分クラスメインページ:ユーザーコントロール {// プライベートブラシ_selectedBrush =新しいSolidColorBrushの(Color.FromArgb(255、255、0、 0)); //プライベートブラシ_normalBrush =新しいSolidColorBrush(Color.FromArgb(255、0、0、0));

public MainPage() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(MainPage_Loaded); 
    } 


    void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     List<Country> list = new List<Country> 
     { 
      new Country{ Name = "USA" }, 
      new Country{ Name = "China"}, 
      new Country{States = GetStates()} 
     }; 

     CmbxCountry.ItemsSource = list; 
     CmbxCountry.DisplayMemberPath = "Name"; 
     } 

    private List<State> GetStates() 
    { 
     List<State> list = new List<State> 
     { 
      new State{ Name = "NJ" }, 
      new State{ Name = "NY" }, 
      new State{ Name = "NV" }, 
      new State{ Name = "CT" }, 
      new State{ Name = "CA" } 

     }; 
     return list; 

    } 
    private void CmbxCountry_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var cmbx = sender as ComboBox; 
     var selectedItem = cmbx.SelectedItem as Country; 

     CmbxState.ItemsSource = selectedItem.States; 
     CmbxState.DisplayMemberPath = "Name"; 
    } 

} 

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

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

} 

}

答えて

0

私のために動作します。最初のコンボボックスで間違った項目を設定しているとします。現在、あなたの「国」コンボボックスは、「USA」、「China」、「」の国で満たされています。 3番目のもの( "")を選択するたびに、空ではない状態リストが得られます。

あなたの現在の実装では、すべての国の状態を持っているしたい場合は、私は次のことをお勧めしたい:

List<Country> list = new List<Country> 
    { 
     new Country{ Name = "USA", States = GetStates() }, 
     new Country{ Name = "China", States = new List<State>() }, 
    }; 

そしてまた、私が持つ、結合してMVVMにこれをリファクタリングお勧めします

public class MainViewModel : INotifyPropertyChanged 
{ 
    private Country _selectedCountry; 

    public ObservableCollection<Country> Countries { get; } 
    public Country SelectedCountry 
    { 
     get { return _selectedCountry; } 
     set { _selectedCountry = value; RaisePropertyChanged("SelectedCountry"); RaisePropertyChanged("States"); } 
    } 

    public State[] States 
    { 
     get 
     { 
      if (_selectedCountry == null) 
       return new State[0]; 
      return _selectedCountry.States.ToArray(); 
     } 
    } 
} 

そして、あなたのビュー(XAML)がイベントを定義し、ちょうどバインディングを使用する必要はありません:選択した国が含まれていますし、それに応じて二コンボボックスの値を更新したモデル。

+0

ありがとうございました。 – user352385

+0

ようこそ。 :) –

関連する問題