I have a list that is Binded with my viewModel.CurrentProduct.ProductAddOns
<ListView ItemsSource="{Binding CurrentProduct.ProductAddOns}" MaxHeight="200">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<ComboBox ItemsSource="{Binding AllAddOns}" SelectedItem="{Binding AddOn}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Text="{Binding Quantity}" Width="100"
Margin="8" VerticalAlignment="Center"/>
</WrapPanel>
</DataTemplate>
ViewModel Code
class AddOrUpdateProductDialogViewModel : BaseViewModel
{
ProductRepository productRepository = new ProductRepository();
TaxRepository taxRepository = new TaxRepository();
CategoryRepository categoryRepository = new CategoryRepository();
AddOnRepository addOnRepository = new AddOnRepository();
public AddOrUpdateProductDialogViewModel()
{
CurrentProduct = new Product
{
Id = 0,
ImageSource = new BitmapImage(new Uri(@"..\..\..\..\assets\Icon.png", UriKind.Relative))
};
FillDefaultData();
}
public AddOrUpdateProductDialogViewModel(int ProductId)
{
CurrentProduct = productRepository.GetById(ProductId);
FillDefaultData();
foreach (var item in AllCategories)
{
var pr = currentProduct.Categories.FirstOrDefault(p => p.Id == item.Model.Id);
if (pr != null)
{
item.IsSelected = true;
}
}
SelectedTax = currentProduct.Tax;
}
private Product currentProduct;
public Product CurrentProduct
{
get { return currentProduct; }
set
{
currentProduct = value;
OnPropertyChanged("CurrentProduct");
}
}
CheckListModel<Category> allCategories;
public CheckListModel<Category> AllCategories
{
get { return allCategories; }
set
{
allCategories = value;
OnPropertyChanged("AllCategories");
}
}
ObservableCollection<Tax> allTaxes;
public ObservableCollection<Tax> AllTaxes
{
get { return allTaxes; }
set
{
allTaxes = value;
OnPropertyChanged("allTaxes");
}
}
Tax selectedTax;
public Tax SelectedTax
{
get
{
return selectedTax;
}
set
{
selectedTax = value;
CurrentProduct.Tax = value;
OnPropertyChanged("SelectedTax");
}
}
ObservableCollection<AddOn> allAddOns;
public ObservableCollection<AddOn> AllAddOns
{
get { return allAddOns; }
set
{
allAddOns = value;
OnPropertyChanged("AllAddOns");
}
}
void FillDefaultData()
{
ICollection<ProductAddOn> addons = CurrentProduct.ProductAddOns;
CurrentProduct.ProductAddOns = new ObservableCollection<ProductAddOn>(addons);
AllAddOns = new ObservableCollection<AddOn>(addOnRepository.GetAll());
AllCategories = new CheckListModel<Category>();
AllTaxes = new ObservableCollection<Tax>(taxRepository.GetAll());
foreach (var item in categoryRepository.GetAll())
{
if (!item.IsDeleted)
{
AllCategories.Add(new CheckListItemModel<Category> { Model = item });
}
}
}
RelayCommand addOrUpdateCommand;
public RelayCommand AddOrUpdateCommand
{
get
{
return addOrUpdateCommand ??
(addOrUpdateCommand = new RelayCommand(o =>
{
currentProduct.Categories = new List<Category>();
currentProduct.Categories = AllCategories.GetChekedItemsModels();
productRepository.AddOrUpdate(CurrentProduct);
MessageBox.Show("Sucessfully Updated", "Updated", MessageBoxButton.OK, MessageBoxImage.Information);
}));
}
}
private RelayCommand addAddOnCommand;
public RelayCommand AddAddOnCommand
{
get
{
return addAddOnCommand ?? (addAddOnCommand = new RelayCommand((o) =>
{
currentProduct.ProductAddOns.Add(new ProductAddOn());
}));
}
}
Inside ListView in dataTemplate, I have ComboBox that I want to bind viewModel.AllAddons.
But Binding AllAdOns
trying to find AllAddOns
property in ProductAddOn class
.
How to bind the AllAddOns from dataContext(viewModel?