开发者

Listbox binding - delayed by one source

开发者 https://www.devze.com 2023-01-18 15:37 出处:网络
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 certai

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消