Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd recommend several changes:</p> <p>First, you might want to break your person class out into two classes: Code and Person</p> <pre><code>public class Person { public int Id { get; set; } public int CodeId { get; set; } public string Name { get; set; } } public class Code { public int Id { get; set; } public string CodeText { get; set; } } </code></pre> <p>Then create a class that you'll set to your view's datacontext, normally called a viewmodel in the MVVM design pattern:</p> <pre><code>public class Data : INotifyPropertyChanged { public List&lt;Person&gt; People { get; set; } public List&lt;Code&gt; Codes { get; set; } private Code _selectedCode; public Code SelectedCode { get { return _selectedCode; } set { _selectedCode = value; PropertyChanged(this, new PropertyChangedEventArgs("SelectedCode")); SelectedPeople = People.Where(p =&gt; p.CodeId == SelectedCode.Id).ToList(); } } private List&lt;Person&gt; _selectedPeople; public List&lt;Person&gt; SelectedPeople { get { return _selectedPeople; } set { _selectedPeople = value; PropertyChanged(this, new PropertyChangedEventArgs("SelectedPeople")); } } public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>Notice that it implements INotifyPropertyChanged. This is crucial with binding in Silverlight. In the constructor of your MainPage.xaml.cs: </p> <pre><code>Data data = new Data { Codes = new List&lt;Code&gt;(),// populate your codes People = new List&lt;Person&gt;() // populate your people, giving them correct codeids } this.DataContext = data; </code></pre> <p>Now in your view's XAMl:</p> <pre><code>&lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Codes}" SelectedItem="{Binding SelectedCode, Mode=TwoWay}" DisplayMemberPath="CodeText" /&gt; &lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="12,54,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" ItemsSource="{Binding SelectedPeople}" DisplayMemberPath="Name" /&gt; </code></pre> <p>Notice the first combobox binds to the Codes collection on the DataContext. The selectedItem property <strong>TWO-WAY</strong> binds to the SelectedCode property on the datacontext. When the user changes that selected item, the setter on the datacontext gets called. We updated the list of peopel to show, and raise the PropertyChanged event which notifies the view that it needs to update the people box.</p> <p>Of course, this would be much cleaner if you properly implemented the MVVM design pattern. I like to use the MVVM Light toolkit for this.</p> <p>I know that was long-winded, but I wanted to guide you down a correct path so you could use this knowledge on this project and in the future :)</p>
 

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