I am new to MVVM, I have been following the article by josh smith , and I am struggling to develop my first attempt. In my case, I have a main window with the main viewing model:
var vm = new MainVM(); MainWindow window = new MainWindow(); window.DataContext = vm;
I have two ItemSuppliersViewModel , SuppliersViewModel view models bound to two ItemSuppliers , SuppliersView datatemplate using the datatemplate in the resourcedictionary main window as follows:
<DataTemplate DataType="{x:Type VM:ItemSuppliersViewModel}"> <VV:ItemSuppliersView/> </DataTemplate> <DataTemplate DataType="{x:Type VM:SuppliersViewModel}"> <VV:SuppliersView/> </DataTemplate>
In the main window, I have a list displaying a list of items attached to:
<ListBox x:Name="ItemsListBox" ItemsSource="{Binding AllItems}" SelectedItem="{Binding SelectedItem}" DisplayMemberPath="Item_Name" />
AllItems is a public property displayed by the Main view model:
public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCriteria.For<Item>()); } }
When the user selects an item from the list, a list of some data related to this item is ItemSuppliers , presented by the ItemSuppliers view model and ItemSuppliersView and displayed in the grid using itemscontrol :
<Grid Margin="246,132,93,94"> <ItemsControl ItemsSource="{Binding ItemSuppliersVM}" Margin="4"/> </Grid>
ItemSuppliersVM displayed in the main view model as follows:
ItemSuppliersViewModel itemSuppliersVM; public ItemSuppliersViewModel ItemSuppliersVM { get { return _itemSuppliersVM; } set { _itemSuppliersVM = value; OnPropertyChanged("ItemSuppliersVM"); } }
Here is the selecteditem property that is bound to the selected list item:
public Item SelectedItem { get { return _selectedItem; } set { _selectedItem = value; OnPropertyChanged("SelectedItem"); ShowItemSuppliers(); } }
the showItemSuppliers that creates the itemsuppliers view model:
void ShowItemSuppliers() { _itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30)); }
The problem is that nothing happened when selecting any item in the list, however the itemsrepository checked and works fine when I, but the breakpoint, all the bindings work, and it looks at the selecteditem property and then showitemsuppliers() .
I think the problem is in this method, so itβs wrong and this method is the right way to instantiate the ItemSuppliersViewModel in the mainwindow view model?