1
votes

I am using MVVM light to bind a ListView to an ItemSource on my ViewModel. When a change is made to the property on the ViewModel the SelectedItem does not update in the View.

XAML:

<ListView Grid.Row="1" SelectionMode="Single" ItemsSource="{Binding filteredAppListItemSource}" SelectedItem="{Binding selectedApp, Mode=TwoWay}">

ViewModel:

public Model.V_PWT_APP_ALL selectedApp { get { return this._selectedApp; } set { this._selectedApp = value; RaisePropertyChanged(() => selectedApp); } }

Selecting an Item from the View updates the ViewModel and all controls that derive their data from SelectedItem are updated. The TwoWay binding does not seem to be working.

2

2 Answers

1
votes

I found that the problem was with the SelectedItem Binding. I found that I wasn't selecting an item correctly. I switch to using SelectedIndex and finding the correct index and the binding works as expected.

0
votes

If you would like to stay with the INotified property changed interface you should set up the code as in the following documentation: https://msdn.microsoft.com/en-us/library/ms184414(v=vs.110).aspx .

As it does not seem like you are changing properties, but rather items and notifying the ItemsSource, a better route would be to implement an IObserver/IObservable interface. This is a little more simple and it keeps updating tidy and manageable inside of the models own class. Here is a possible code example:

public class App: IObservable<App>     //where the model class is the IObserver.

{

private App app;
internal App
List<IObserver<App>> Observers = new List<IObserver<App>>();

{

     get:
            {

                 return app;
            }

     set:

            {
                 app = value;
                 try
                 {
                      foreach (var observer in Observers) observer.OnNext(value);
                      foreach (var observer in Observers) observer.OnCompleted();
                 }
                 catch(Exception ex)
                 {
                      foreach (var observer in Observers) observer.OnError(ex);
                      throw;
                 }
            }
}

For the viewModel:

public Model.V_PWT_APP_ALL selectedApp: IOberver<App>
{
     List<Control> ControlsToBeUpdated = new List<Control>() //Each control that needs updated should implement and interface that has an .Update(type T) method where T is whatever value needs updated.
     public void OnCompleted()
     {
          //...notify taskBar, etc. 
     }

     public void OnError(Exception error)
     {
         //handle exception, update log, etc.
     }

     public void OnNext(object value)
     {
         foreach (var control in ControlsToBeUpdated) control.Update(value);
     }
}

This might seem tedious at first, but it has helped me minimize and manage updates and bugs in ItemsSource quite nicely. Finally, I have found controlling the scrollView rather tedious when working with the ListView ItemsSource, and have had better luck with simply using ListView.Items.Insert(...) method as when the ItemsSource is updated and a scrollview has been selected it has often thrown an OutOfRange exception. This can be difficult debugging in MVVM. However, this is another topic. I hope this helps.