I am new to MVVM so exuse me if this is very simple question
i have a listbox that display a list of Categories types for products when the user select any category type, a list of total expenses for each category of the selected category type is displayed as a view into an expander:
<Expander Header="{Binding ElementName=cattypeslistbox,Path=SelectedItem.Cat_Type}"
Visibility="{Binding Path=ExpanderIsVisible,Mode=TwoWay}"
IsExpanded="{Binding Path=ExpanderIsExpanded,Mode= TwoWay}" Height="167" Name="expander1" Width="357" >
<Border Background="GhostWhite" BorderBrush="LightGray" BorderThickness="1" CornerRadius="5">
<ItemsControl ItemsSource="{Binding CurrentViewModel,Mode=TwoWay}" Margin="4" />
</Border>
</Expander>
in my main viewmodel i have currentviewmodel as viewmodelbase:
VMBase _currentViewModel;
public VMBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
_currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
public Category_Types SelectedCategoryTypes
{
get
{
return _selectedCategoryTypes;
}
set
{
_selectedCategoryTypes = value;
OnPropertyChanged("SelectedCategoryTypes");
ShowCatsTotalExpenses();
}
}
void ShowCatsTotalExpenses()
{
CatsTotalExpensesVM vm = new CatsTotalExpensesVM(_categoryTypesRepository, _selectedCategoryTypes, _from, _to);
_currentViewModel = vm;
ExpanderIsVisible = Visibility.Visible;
ExpanderIsExpanded = true;
}
the problem is that this doesn't work nothing is displayed in the expander;
but when define a list of VieModels as following:
ObservableCollection<VMBase> _viewModels;
public ObservableCollection<VMBase> ViewModels
{
get
{
if (_viewModels == null)
_viewModels = new ObservableCollection<VMBase>();
return _viewModels;
}
}
and in the ShowCatsTotalExpenses()
CatsTotalExpensesVM vm = new CatsTotalExpensesVM(_categoryTypesRepository, _selectedCategoryTypes, _from, _to);
_viewModels.Clear();
_viewModels.Add(vm);
then when binding to thses list of viewmodels it works fine and display the view:
<ItemsControl ItemsSource="{Binding ViewModels,Mode=TwoWay}" Margin="4" />
So what is the problem with the first CurrentViewModel how to get it work and what i am missing.
Thanks;