Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, firstly, is there any reason you're using a ListView over a ListBox? If you're not sure which to use, I would use a ListBox. Have one ListBox bound to your list of dealers sitting on your view model, which would be an ObservableCollection if you're going to add/remove dealers. Then bind the SelectedValue of that ListBox to a Dealer property on your view model, called perhaps SelectedDealer. </p> <p>Then have a second ListBox which shows the list of cars for the selected dealer. Bind the DataContext property of the car ListBox to the SelectedDealer, and bind the ItemsSource to Cars. Then your car ListBox will be bound to the collection of cars sitting on the selected Dealer. </p> <p>If you want to add/remove cars and have the UI updated, then you'll want to use an ObservableCollection. You don't want your business objects returning ObservableCollections, so this conversion should be done on your view models. You could have an ObservableCollection property on your view model called SelectedCars perhaps, and bind your car list box to this. In the setter for SelectedDealer, you could do something like:</p> <pre><code>public ObservableCollection&lt;Car&gt; SelectedCars { get; set; } private Dealer selectedDealer; public Dealer SelectedDealer { get { return this.selectedDealer; } set { if (this.selectedDealer != value) { this.selectedDealer = value; this.SelectedCars = new ObservableCollection(this.SelectedDealer.Cars); OnPropertyChanged(() =&gt; this.SelectedDealer); } } } </code></pre> <p>In this case you wouldn't set the DataContext of your car listbox to the SelectedDealer, the DataContext wouldn't be set, and the view model would be used implicitly. Then just bind the ItemsSource of the car listbox to the SelectedCars property.</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