What is the preferred way to bind a List property to an object in WPF?

I am using MVVM in WPF in C # .NET. Let's say I have the following sample model:

public class Dealer
{
    public string Name { get; set; }
    public List<Car> Cars { get; set; }
}

public class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
}

In my opinion, I have a list of dealers, and if I select a dealer in ListView, then I should look at the details of the dealer, including the name of the dealer and the list of cars.

My question is: how to associate a list of cars with a list view, given that this is a property of a Dealer object? In particular, what should I have in my model? I need a list of cars that need to be updated if I add something to the collection, so I feel like I will need an ObservableCollection. However, this apparently leads to the presence of two collections (list and observed set) for each dealer, and with this duplication of data, it seems inefficient in terms of coding and performance. Any ideas?

+3
source share
3 answers

, , ObservableCollection. ObservableCollection ViewModel Child, ViewModel , . , . , . , MVVM , , , , Child, , Child .

public class DealerViewModel : ViewBase
{
    // This concept can be wrapped in a ListViewBase for a more DRY approach
    private ObservableCollection<CarViewModel> cars;
    public ObservableCollection<CarViewModel> Cars
    {
        get { return cars; }
        set {
            if (cars == value) return;
            cars = value;
            RaisePropertyChanged("Cars");
        }
    }

    // This concept can be wrapped in a ListViewBase for a more DRY approach
    private CarViewModel selectedCar;
    public CarViewModel SelectedCar
    {
        get { return selectedCar; }
        set {
            if (selectedCar== value) return;
            selectedCar= value;
            RaisePropertyChanged("SelectedCar");
        }
    }

    public DealerViewModel(Dealer dealer)
    {
        // This concept can be wrapped in a ListViewBase for a more DRY approach
        Cars = new ObservableCollection(dealer.Cars.Select(c => new CarViewModel(c)));
    }
}

public class CarViewModel
{
}
+2

, -, , ListView ListBox? , , ListBox. ListBox , , ObservableCollection, / . SelectedValue ListBox Dealer , SelectedDealer.

ListBox, . DataContext ListBox SelectedDealer ItemSource Cars. ListBox , .

/ , ObservableCollection. , - ObservableCollections, . , ObservableCollection SelectedCars . SelectedDealer - :

public ObservableCollection<Car> 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(() => this.SelectedDealer);
    }
  }
}

DataContext SelectedDealer, DataContext , . ItemSource SelectedCars.

+2

personaly "cars" exaple "Stock". , . ienumerable Stock, . INotifyPropertyChanged. Stock , IEnumerable .

0

Source: https://habr.com/ru/post/1780320/


All Articles