2012-04-01 6 views
0

Ich habe meinen Code sowohl XAML und C#, aber es funktioniert nicht. Bitte beraten. Dankwie 2 Combox miteinander interagieren 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> 

Namespace Survey_Model { public partial class Mainpage: Usercontrol { // Private Pinsel _selectedBrush = new SolidColorBrush (Color.FromArgb (255, 255, 0, 0)); // private Brush _normalBrush = neuer 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; } 

} 

}

Antwort

0

Arbeiten für mich. Ich nehme an, dass Sie in der ersten Combobox den falschen Gegenstand setzen. Derzeit ist Ihre Combo-Box "Country" mit folgenden Ländern gefüllt: "USA", "China", "". Immer wenn Sie den dritten ("") auswählen, erhalten Sie eine nicht leere Statusliste.

Wenn Sie in der aktuellen Implementierung für jedes Land Staaten haben möchten, würde ich vorschlagen, folgende:

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

Und auch würde ich Refactoring diese in eine MVVM empfehlen mit Bindung, eine mit Modell, die ausgewählte Land enthält und aktualisiert zweiten Combobox Wert entsprechend:

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

und Ihre Ansicht (xAML) nicht Ereignisse definieren muß und verwenden nur verbindlich.

+0

Vielen Dank. – user352385

+0

Gern geschehen. :) –