peterh
Topic Author
Posts: 39
Joined: 13 Mar 2015, 13:50

ListView does not update properly

14 Sep 2017, 11:31

I have a ListView like this:
<ListView ItemsSource="{Binding MPValues}" >
  <ListView.View>
    <GridView>
      <GridViewColumn  Header="#" DisplayMemberBinding="{Binding LiteralIndex}"/>
      
      <GridViewColumn >
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <Button  Command="{Binding Path=DataContext.DeleteCommand, ElementName=DetailsController}" CommandParameter="{Binding LiteralIndex}" Content="Delete" >
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>
The DeleteCommand removes an element from the underlying MPValues collection and notifies that the MPValues property has changed.
I have confirmed that the element gets deleted correctly.

However, the change in the ListView does not show up until I navigate away from the ListView and then back again.

How can I force a redraw of the ListView so that the changes show up for the user immediately after the change has happened?

I have seen ListView.Items.Refresh(); used in WPF applications, but that does not seem to work here.
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ListView does not update properly

19 Sep 2017, 12:09

Hi,

I tried to reproduce your problem but it is working fine for me, when Delete button is clicked, the item is correctly removed from the ListView.
I tested with NoesisGUI 2.0.2f2 and with latest beta 2.1.0b3, using the following code along with the xaml you posted:
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
using Noesis.Samples;

public class Item
{
    public int LiteralIndex { get; set; }
}

public class Context
{
    public DelegateCommand DeleteCommand { get; private set; }
    public ObservableCollection<Item> MPValues { get; private set; }
    
    public Context()
    {
        DeleteCommand = new DelegateCommand(OnDelete);
        MPValues = new ObservableCollection<Item>();
        
        for (int i = 0; i < 20; ++i)
        {
            MPValues.Add(new Item { LiteralIndex = i });
        }
    }
    
    private void OnDelete(object param)
    {
        int literalIndex = (int)param;
        int numItems = MPValues.Count;
        for (int i = 0; i < numItems; ++i)
        {
            Item item = MPValues[i];
            if (item.LiteralIndex == literalIndex)
            {
                MPValues.RemoveAt(i);
                break;
            }
        }
    }
}

public class TestBehavior : MonoBehaviour
{
    void Start()
    {
        var gui = GetComponent<NoesisView>();
        gui.Content.DataContext = new Context();
    }
}
Is there anything different from what you are doing?
I'm using the default ListView and ListViewItem templates here, are you defining your own?
 
peterh
Topic Author
Posts: 39
Joined: 13 Mar 2015, 13:50

Re: ListView does not update properly

25 Sep 2017, 11:45

I realized this was my own mistake. The list I bound to was not (and could not be) an ObservableCollection. I solved it by wrapping and re-wrapping the bound collection when the underlying collection changed.
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ListView does not update properly

25 Sep 2017, 17:28

Ok, that makes sense, if the items source wasn't an ObservableCollection then it couldn't notify of changes.
I'm marking this as solved.

Who is online

Users browsing this forum: No registered users and 1 guest