Genom
Topic Author
Posts: 47
Joined: 04 Dec 2014, 14:47

PropertyChanged in Thread not working properly

18 Dec 2015, 18:58

Hi, after upgrading to 1.2.5f8 (the same with 1.2.5f9) I have problems with PropertyChanged in a ViewModel that I use for template within a ListBox.

As I am receiving data asynchronously and then assigning to a property of the view model (launching the NotifyPropertyChanged) it should have refreshed it in the view, but this is not happening always, just randomly.
<ListBox x:Name="LvOrders" SelectedItem="{Binding SelectedOrder}" ItemsSource="{Binding Orders}">
                <ListBox.Resources>
                  <DataTemplate DataType="{x:Type local:TrackedOrderViewModel}"><Grid Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Left" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}">
                        <TextBlock Text="{Binding WholeAddress}" FontSize="{Binding FontSize}" Foreground="{Binding Foreground}" Margin="{Binding MarginSize}" HorizontalAlignment="Left" TextWrapping="Wrap" />
                      </Grid>                
                    
                </DataTemplate>
              </ListBox.Resources>
            </ListBox>
public class TrackedOrderViewModel : ViewModelBaseUnity
{
        private Address OrderAddressTo
        {
            get { return _addressTo; }
            set
            {
                _addressTo = value;
                City = _addressTo.City;
                Street = _addressTo.Street;
                NotifyPropertyChanged("WholeAddress");
            }
        }

        public TrackedOrder Order
        {
            get { return _order; }
            set
            {
                _order = value;
                _order.AddressTo.OnLoadedAction = o =>
                {
                    OrderAddressTo = o as Address;
                };
            }
        }

        public string WholeAddress
        {
            get
            {
                return string.Format("{0}. {1}", City, Street); 
            }
        }

}

As you can see in the assign of the order, the OnLoadedAction is an action that would be called within a Thread, and so it is, but the PropertyChanged event don't update the view always (it is random).

do you have any idea why? I think to have seen in Trello that something was refactored about this, so maybe you can quickly figure out... Other wise I'll try to provide more information.

regards
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: PropertyChanged in Thread not working properly

19 Dec 2015, 11:51

Hi,

Updating the UI tree from a thread is not supported. It may work sometimes when main thread is not accessing the data at the same time, but you shouldn't notify of property changes (that might produce modifications to UI elements) in a separate thread.

Maybe is a good idea to have an INotifyPropertyChanged base class that deals with thread access, something similar to what is explained in this article: http://www.zagstudio.com/blog/1398#.VnU1j_nNxaQ

Anyway, we should add some kind of check when UI is accessed from different threads. I will add this task to our trello board.

Let me know if you need more help.

Who is online

Users browsing this forum: No registered users and 1 guest