ListView does not update properly
I have a ListView like this:
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.
Code: Select all
<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>
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.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ListView does not update properly
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:
Is there anything different from what you are doing?
I'm using the default ListView and ListViewItem templates here, are you defining your own?
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:
Code: Select all
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();
}
}
I'm using the default ListView and ListViewItem templates here, are you defining your own?
Re: ListView does not update properly
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.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ListView does not update properly
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.
I'm marking this as solved.
Who is online
Users browsing this forum: No registered users and 1 guest