简体   繁体   中英

How to Select comboBox values in Selection Changed event

I have 3 ComboBoxes in WPF form...

When i am selecting 1st combox Box values its fill 2nd ComboBox...

but while selecting 1st time comboxBox values its SelectionChanged Event is not working... After selecting 2nd time its working....

 private void cmbBoard_SelectionChanged(object sender, SelectionChangedEventArgs e)
    { cmbClass.ItemsSource = dt.DefaultView;
                    cmbClass.DisplayMemberPath = "Class";
                    cmbClass.SelectedValuePath = "ClassID";
                    cmbClass.SelectedIndex = 0;
    }

here is a more detailed example:

I wrote an example with students and classes. You have a class which has a name and a collection of students. And each student has a name. My classes:

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

  public Collection<Student> Students
  {
    get;
    set;
  }

  public Class( string name, Collection<Student> students )
  {
    this.Name = name;
    this.Students = students;
  }
}

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

  public string FirstName
  {
    get;
    set;
  }

  public string Fullname
  {
    get
    {
      return string.Format( "{0}, {1}", this.Name, this.FirstName );
    }
  }

  public Student(string name, string firstname)
  {
    this.Name = name;
    this.FirstName = firstname;
  }
}

My from contains tow comboboxes and i want that the first one contains all classes and the second one should contains all students which are in the selected class. I solved this (quick and dirty) in the code behind. I wrote a property for all my classes and mock some data. Here are the important parts:

public Collection<Class> Classes
{
  get;
  set;
}

public MainWindow()
{
  this.InitializeComponent( );
  this.Classes = new Collection<Class>( )
  {
    new Class("Class 1",
      new Collection<Student>()
      {new Student("Caba", "Milagros"),
        new Student("Wiltse","Julio"),
        new Student("Clinard","Saundra")}),

    new Class("Class 2",
      new Collection<Student>()
      {new Student("Cossette", "Liza"),
        new Student("Linebaugh","Althea"),
        new Student("Bickle","Kurt")}),

    new Class("Class 3",
      new Collection<Student>()
      {new Student("Selden", "Allan"),
        new Student("Kuo","Ericka"),
        new Student("Cobbley","Tia")}),
  };
  this.DataContext = this;
    }

Now I create my first combobox with the name cmbClass.

<ComboBox x:Name="cmbClass"
              ItemsSource="{Binding Classes}"
              DisplayMemberPath="Name"
              Margin="10"
              Grid.Row="0"/>

After that I create my second combobox. But to get the itemsSource, i need the value from the first box. So i use an element binding to get the value from the first box.

Binding ElementName=cmbClass

And i am interested at the SelectedItem of the first box because this item contains a collection of all needed students(see the class above). So i use the path property to solve this. My second combobox:

<ComboBox ItemsSource="{Binding ElementName=cmbClass, Path=SelectedItem.Students}"
          DisplayMemberPath="Fullname"
          Margin="10"
          Grid.Row="1"/>

Finish!!!

Hope this more detailed solution helps you to understand my way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM