Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here is a more detailed example:</p> <p>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:</p> <pre><code>public class Class { public string Name { get; set; } public Collection&lt;Student&gt; Students { get; set; } public Class( string name, Collection&lt;Student&gt; 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; } } </code></pre> <p>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:</p> <pre><code>public Collection&lt;Class&gt; Classes { get; set; } public MainWindow() { this.InitializeComponent( ); this.Classes = new Collection&lt;Class&gt;( ) { new Class("Class 1", new Collection&lt;Student&gt;() {new Student("Caba", "Milagros"), new Student("Wiltse","Julio"), new Student("Clinard","Saundra")}), new Class("Class 2", new Collection&lt;Student&gt;() {new Student("Cossette", "Liza"), new Student("Linebaugh","Althea"), new Student("Bickle","Kurt")}), new Class("Class 3", new Collection&lt;Student&gt;() {new Student("Selden", "Allan"), new Student("Kuo","Ericka"), new Student("Cobbley","Tia")}), }; this.DataContext = this; } </code></pre> <p>Now I create my first combobox with the name cmbClass.</p> <pre><code>&lt;ComboBox x:Name="cmbClass" ItemsSource="{Binding Classes}" DisplayMemberPath="Name" Margin="10" Grid.Row="0"/&gt; </code></pre> <p>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.</p> <pre><code>Binding ElementName=cmbClass </code></pre> <p>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:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding ElementName=cmbClass, Path=SelectedItem.Students}" DisplayMemberPath="Fullname" Margin="10" Grid.Row="1"/&gt; </code></pre> <p>Finish!!!</p> <p>Hope this more detailed solution helps you to understand my way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload