Silverlight 3 Child Resource Property Not Updated

I have a Silverlight control with my root ViewModel as a data source. ViewModel provides a list of maps, as well as a SelectedCard property, which is associated with a drop-down list at the top of the view. Then I have a view view that displays SelectedCard properties. My XAML looks like (reduced for simplicity):

<StackPanel Orientation="Vertical"> <ComboBox DisplayMemberPath="Name" ItemsSource="{Binding Path=Cards}" SelectedItem="{Binding Path=SelectedCard, Mode=TwoWay}" /> <TextBlock Text="{Binding Path=SelectedCard.Name}" /> <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Path=SelectedCard.PendingTransactions}" /> </StackPanel> 

I would expect TextBlock and ListBox to be updated whenever I select a new item in ComboBox, but this is not the case. I am sure that this is due to the fact that TextBlock and ListBox are actually tied to SelectedCard properties, so it listens for property change notifications for the properties of this object. But I would think that the data binding would be smart enough to recognize that the parent in the binding expression has changed and updated the entire binding.

It should be noted that the PendingTransactions property (associated with the ListBox) is loaded with a lazy load. So, the first time I select an item in ComboBox, I make an asynchronous call and load the list and user interface updates to display the information corresponding to the selected item. However, when I re-select an item, the user interface does not change!

For example, if my initial list contains three cards, I select the first card by default. The data binding attempts to access the PendingTransactions property in this map object and updates the ListBox correctly. If I select the second card in the list, the same thing happens, and I get the PendingTransactions list for this card. But, if I select the first card again, nothing changes in my user interface! Setting a breakpoint, I can confirm that the SelectedCard property is updated correctly.

How can I do this job?

+4
source share
3 answers

It turns out that the problem is not in the user interface at all. The PendingTransactions lazy class uploads its values ​​using an async WCF call to the server. The asynchronous pattern uses events to notify the caller that the operation is complete so that data can be parsed in the class. Since each card has its own instance of the PendingTransactions class, and we used ServiceFactory to manage WCF proxies, each instance connected its event handler to the same event (we use a singleton approach for performance reasons - for now), so each instance received an event every time any of the instances started the async operation.

This means that data binding is working correctly. PendingTransactions collections rewrote themselves every time a new map is viewed. Thus, it turned out that the choice of the previous map did nothing, when in fact he chose the right object for binding, it was data that was screwed up and made it look as if nothing had changed.

Thanks for the tips and tricks nonetheless!

0
source

If you are using Silverlight 3, you will need to use INotifyPropertyChanged.

Example:

 public class CardViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public ObservableCollection<Card> Cards { get; set; } private Card _selectedCard; public SelectedCard { get { return _selectedCard; } set { if (value != _selectedCard) { _selectedCard = value; NotifyPropertyChanged("SelectedCard"); } } } public CardViewModel() { Cards = new ObservableCollection<Card>(); //Populate Cards collection with objects } public void NotifyPropertyChanged(string item) { if (PropertyChanged!=null) { PropertyChanged(this, new PropertyChangedEventArgs(item)); } } } 

All you have to do is set this class to your DataContext views, and everything should be happy.

0
source

The example I used recently is to bind the data context of the detailed information container to the selected list item. XAML in your case becomes:

 <StackPanel Orientation="Vertical"> <ComboBox x:Name="_lbxCards" <-- new DisplayMemberPath="Name" ItemsSource="{Binding Path=Cards}" SelectedItem="{Binding Path=SelectedCard, Mode=TwoWay}" /> <StackPanel DataContext={Binding ElementName=_lbxCards,Path=SelectedItem}> <-- new <TextBlock Text="{Binding Path=Name}" <-- updated /> <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Path=PendingTransactions}" <-- updated /> </StackPanel> <-- new </StackPanel> 
0
source

Source: https://habr.com/ru/post/1307354/


All Articles