Click here to Skip to main content
14,738,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
Posted
Comments
theHollow 14-Sep-11 16:37pm
   
The ShowCatsTotalExpenses method will only be executed once, when the SelectedCategoryTypes property is set the first time. Not when it changes.

In order to also get the changes, you should create a DependencyProperty and create an "OnSelectedCategoryTypesChanged" method that fire on change.

The "ViewModels" property should also be notified with OnPropertyChanged.

But I don't know if this is in any help to the actual problem :-)

The problem you are having, is that ItemsSource needs a list to bind to. In your first example, you are just binding to an object, which has no IEnumerator defined. Basically, the ItemsControl would not know how to show anything so it doesn't.

This is why your second example works. After this has been applied, what problem remains?
   
The easiest way you going to be able to pinpoint your problem with the binding is to set a trace on it. by setting it up like this:

<window xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" />


then
diag:PresentationTraceSources.TraceLevel=High
or where ever you want to check a binding.

<expander header="{Binding ElementName=cattypeslistbox,Path=SelectedItem.Cat_Type, diag:PresentationTraceSources.TraceLevel=High}">
                      Visibility="{Binding Path=ExpanderIsVisible,Mode=TwoWay}"
                      IsExpanded="{Binding Path=ExpanderIsExpanded,Mode= TwoWay}" Height="167" Name="expander1" Width="357" >
</expander>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900