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"  Foregr开发者_StackOverflow中文版ound="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.
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.
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.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论