Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I am using wcf duplex communication in my project and i have implemented callback method in my Viewmodel. Data is coming to my viewmodel but it is not adding to Observable collection. it is always showing count ass Zero.

Application.Current.Dispatcher.BeginInvoke(
 new Action(() =>
 {
     FromCallBack = new LogEvents()
     {
         DateAndTime = DateTime.Now.ToString()
     };
     clctionlogs.Add(FromCallBack);

 }));


What I have tried:

I have tried with Dispatcher and SendOrPostCallback threading concepts but still data is not adding to collection.

If i try to popup a message box with same incoming data it is working fine.
Posted
Updated 8-Jan-19 2:40am
Comments
TABiSH777 8-Jan-19 8:00am    
Hi, its really difficult to answer you from the detail that you have provided, but there can be following issues,
1) please check names & their convention, & whether you are adding in the same collection or not.
2) please check if you are even initializing that observable collection or not, other wise it may be giving object reference... error.

Basically you need to put a debugger over your observable collection property & check whether or not its been even called or not at the time of adding.

It's difficult to tell from the code posted but a common mistake made that breaks Data Binding is to reinitialize an ObservableCollection rather than clearing it after the data binding is set.

So the mistake is to do this:
C#
public class MyViewModel
{
    public ObservableCollection<string> Messages { get; }

    public MyViewModel()
    {
        Messages = new ObservableCollection<string>();
    }

    public void GetData()
    {
        Messages = new ObservableCollection<string>();  // mistake
        //Retrieve data code goes here...
    }
}

The correct method is as follows:
C#
public class MyViewModel
{
    public ObservableCollection<string> Messages { get; }

    public MyViewModel()
    {
        Messages = new ObservableCollection<string>();
    }

    public void GetData()
    {
        Messages.Clear(); // correct
        //Retrieve data code goes here...
    }
}
 
Share this answer
 
v2
Comments
Member 13940788 9-Jan-19 1:34am    
Forget about everything. A data which is coming from wcf(duplex service) callback method(which is implemented at client side that is inside My ViewModel), is not binding to UI.
Graeme_Grant 9-Jan-19 4:56am    
Do you see any Data Binding errors in the Output Window?

We can't see your project on your screen from here...

The code in your question does not show any Xaml, how you connect the ViewModel to the DataContext, what collection class that you are using, how you are filling the collection itself, nor the how you implement the model to hold the data in the collection.

Please update your question with more information please.
Hi,

you need to ensure that your binding is working correctly - for that, look at the output window - is there a BindingExpression Error that you might need to fix?

If thats not the case, review your binding in the UI control eg. ListBox should look something like this:

XML
<ListBox ItemsSource="{Binding WCFItemsList}"/>


where WCFItemsList should be your implementation of the observable Collection in the bound ViewModel:

C#
public property ObservableCollection<MyWCFItems> WCFItemsList
{
  get
  {
    return _WCFItemsList;
  }
}


Beyond from this, you should also test with debugger breakpoints in the viewmodel to ensure that your observable collection is loaded correctly.

You might not see the count in the UI because its not an INotifyPropertyChanged property?

Its realy hard to say anything specific since you give no useful source code sample but I hope these checkpoints help you investigate the issue ...
 
Share this answer
 
Comments
Member 13940788 8-Jan-19 7:47am    
Here is my Observable collection

public ObservableCollection<logevnts> ClctionEventLogs
{
get
{
return _collectionEventLogs;
}
set
{
_collectionEventLogs = value;
OnPropertyChanged("ClctionEventLogs");
}

}
Graeme_Grant 8-Jan-19 8:44am    
No need to implement INotifyPropertyChanged for ObservableCollection as it implements ICollectionChanged internally.
Dirk Bahle 8-Jan-19 10:11am    
I only meant the INotifyPropertyChanged is necessary if you want to display the count in the UI. But thats just guess work since the provided code sample does not show what was tried so far ...
Graeme_Grant 8-Jan-19 17:45pm    
No, you don't need to as the Count property is internal to the ObservableCollection class. If you peek at the ObservableCollection class you can see it is already implemented:
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900