views:

72

answers:

2

hey. I'm trying to maintain an MVVM model, but am tripping up somewhere when trying to bind to a listbox. The first time I run the application, I set ItemCatalog (ObservableCollection) to be a certain ObservableCollection list. The listbox is empty, but if I try again it works. However, there is a lag between the datasource and the displayed data. For example,

I load the app -> set source to X -> Listbox is empty

I now set source to Y -> Listbox displays X

I set source to Z -> Listbox displays Y etc..

This, to me, implies the binding is working but I can't figure out why it's lagging by one.

My XAML looks like this:

<ListBox ItemsSource="{Binding ItemCatalog}">
       <DataTemplate>
             <StackPanel Margin="0,0,0,17" Width="432">
                  <TextBlock Text="{Binding Ref}" TextWrapping="Wrap"  Foreground="Black" />
                  <TextBlock Text="{Binding ItemName}" TextWrapping="Wrap" Margin="12,-6,12,0" Foreground="Black" />
             </StackPanel>
       </DataTemplate>
</ListBox>

In the ViewModel, the value of ItemCatalog is based on whichever button the user pressed (X,Y,Z), which are all stored in a static variable. Is there a reason there's a delay, or a way to force it to rebind to the latest ItemCatalog value?

If I remove the binding from the XAML and set the listbox source straight from the code-behind, it works perfectly. However, I want to try and maintain the XAML coding. I'm new to MVVM so I might be doing this the wrong way round. thanks

EDIT - extra code

The code is changed in the OnNavigatedTo method

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {


            if (NavigationContext.QueryString["Category"].ToString() == "House")
            {
                _categoryType = Models.House;

            }
            if (NavigationContext.QueryString["Category"].ToString() == "Car")
            {
                _categoryType = Models.Car;

            }

            MyViewModel.LoadNewData(_categoryType);


            base.OnNavigatedTo(e);
        }

LoadNewData method just sets the static variable based on the parameter passed.

   ItemCatalog = App.CarCatalog;

Essentially, I need to rebind after the page has loaded, via XAML.

A: 

You don't show any code on where you change source from X to y but I would assume from the explained behavior that the data binding on your view is being interpreted before that change is taking place.

Roadie57
Updated the question with some code.
SSL
Is ItemCatalog an ObservableCollection?
Roadie57
@Roadie57 - I managed to solve it (check accepted answer), but yes, ItemCatalog is an ObservableCollection.
SSL
+2  A: 

At guess (since there still isn't really enough code to go on) your ViewModel is raising a property change even before the field backing ItemCatalog is actually assigned. Something like:-

 ObservableCollection<Stuff> _ItemCatalog;
 ObservableCollection<Stuff> ItemCatalog 
 {

    get { retutn _ItemCatalog; }
    set
    {
       PropertyChanged("ItemCatalog");
       _ItemCatalog = value;
    }
 }

This might result in the behaviour you describe.

AnthonyWJones
That's a great shot in the dark! :D I moved the NotifyPropertyChange method and in the View, I put the DataContext within the `PropertChanged` event of the ViewModel. I thought the DataContext would only need to be set the once, but it seems that I had to set it everytime the PropertyChange event is raised. Thanks!
SSL