简体   繁体   中英

Why Observable Collection is updating the ListView only when event is fired

I am trying to display user made operations that will be stored into Observable collection and displayed using ListView . My problem is that Observable collection is updating only when there is fired an event or command.. If i'm just simply calling that AddItemsToList() method using wcf service callback, then it doesn't work.. I'm binding Datacontext in my window.xaml.cs constructor not in xaml. My Observable collection is stored into my view model.

My code:

AgentWindow.xaml.cs

public AgentWindow()
{
    InitializeComponent();

    var agentViewModel = new AgentViewModel();
    //binding list itemsource to observable collection
    lstOperations.ItemsSource = agentViewModel.OperationList;
    store view model into data context
    this.DataContext = agentViewModel;
}

AgentWindows.xaml

<ListView x:Name="lstOperations">
    <ListView.View>
        <GridView x:Name="grdOperations">
            <GridViewColumn Header="username" DisplayMemberBinding="{Binding Username}"/>
            <GridViewColumn Header="function name"  DisplayMemberBinding="{Binding FunctionName}"/>
        </GridView>
    </ListView.View>
</ListView>

AgentViewModel.cs

public class AgentViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Operation> OperationList;

    public AgentViewModel()
    {
        Initialize_Commands();
        this.OperationList = new ObservableCollection<Operation>();
    }

    //Method that will be used to store new information into list 
    public void AddItemsToList()
    {
        OperationList.Add(new Operation 
        { 
            Username = "SomeUsername", 
            FunctionName = "SomeFunction" 
        });
    }

    //Singleton for this class
    private static AgentViewModel _instance;
    public static AgentViewModel Instance
    {
        get
        {
            return _instance ?? (_instance = new AgentViewModel());
        }
    }

    //event that is not used
    public event PropertyChangedEventHandler PropertyChanged;
}

Calling that AddItemsToList() method from wcf callback method

 AgentViewModel.Instance.AddItemsToList();

The problem is here:

public AgentWindow()
{
    InitializeComponent();

    // Don't create a **new** VM
    //var agentViewModel = new AgentViewModel();

    // Get your main instance 
    var agentViewModel = AgentViewModel.Instance;

    //binding list itemsource to observable collection
    lstOperations.ItemsSource = agentViewModel.OperationList;
    store view model into data context
    this.DataContext = agentViewModel;
}

Since you're using a singleton, you need to use that singleton in your View binding as well. Right now, you're adding items to a different VM instance than the one you're displaying.

On a side note:

Calling that AddItemsToList() method from wcf callback method

Any time you add items to an ObservableCollection<T> , you need to do it on the main (UI) thread. I suspect your WCF callback is happening on a threadpool thread, which could prevent some of the INotifyCollectionChanged mechanisms from working properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM