0

Hello I have this Listview GridView in XAML

<ListView FontSize="10" Name="List_To_Lock" Grid.Row="3"  Background="DarkGray" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="4" Margin="4">
    <ListView.View>
        <GridView  >
            <GridView.Columns>
                <GridViewColumn  Header="No1" Width="40" DisplayMemberBinding="{Binding No1}"/>
                <GridViewColumn Header="No2" Width="150" DisplayMemberBinding="{Binding No2}"/>
                <GridViewColumn Header="No3" Width="280" DisplayMemberBinding="{Binding No3}"/>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

and I have this class

public class numbz
{
    public string No1 { get; set; }
    public string No2 { get; set; }
    public string no3 { get; set; }


}

and in the MainWindow class I done this

List<numbz> num = new List<numbz>();
private void AddToList(string path)
{
    string a1= "a1"+path;
    string b1= "b1"+path;
    string c1= "c1"+path;
    num.Add(new numbz() { No1 = a1, No2 = a2, No3 = a3 });
}

private void RfrshLstVw()
{
    List_To_Lock.ItemsSource = num;
}

 private void Add_Click(object sender, RoutedEventArgs e)
 {

    AddToList(textbox.text);
    RfrshLstVw();
 }

when I click the add button which call the latest method above it add the items on the listview but when I change the content of the textbox and there is one item in the listview it won't add the next item. How can I fix that?

0

2 Answers 2

3

As you are assigning the same collection to the items source again, WPF will not refresh the ListView. There are different ways to achieve what you want, but the best one is to use the ObservableCollection<T> instead of List<T>. In this case you even do not need your refresh method, as the ListView will be updated automatically as soon as you add a new item to the observable collection.

Sign up to request clarification or add additional context in comments.

Comments

2

To expand on Pavel's answer, WPF is listening for events that come from either an INotifyPropertyChanged (in the case of the thing holding the list) or from an INotifyCollectionChanged (in the case of the list itself).

In the spirit of MVVM, I've used ViewModels that implement the INotifyPropertyChanged interface, and assigned those to the View's DataContext property, so that when e.g. a ListView is bound to a list inside the view (i.e. <ListView ItemsSource="{Binding ListProperty}"/>) then WPF will see that the ViewModel is an INotifyPropertyChanged. Then it'll begin listening for when a property changes.

But even with that, WPF won't pick up on the changes inside the list, only to changes when you swap out the list itself. If you want to get it to pick up changes inside the list, then you have to make sure the property you've bound to returns a different instance of a list each time it's accessed.

For example, by making the list property generate a new IEnumerable each time it's accessed:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private readonly List<string> _internalList;
    public IEnumerable ListProperty => _internalList.AsEnumerable();
    // Other code here, including code to raise the "PropertyChanged" event when you want WPF to refresh any bindings to the ListProperty
}

...then when the ViewModel fires off the PropertyChanged event for the ListProperty property, WPF will look at it again, see that it's a different reference, and will refresh itself with the new IEnumerable.


But the best route is to use an ObservableCollection, as he said. That is much more efficient when you have tons of items in the list... then WPF will cherry pick the changes instead of having to refresh the whole list.

The way an ObservableCollection works is by implementing the INotifyCollectionChanged interface. It raises an event anytime anything in the list changes (e.g. when something is added, moved, replaced, or moved). WPF will see that the list is an INotifyCollectionChanged and will automatically begin listening for that event.

That event tells WPF exactly what has changed where, which saves it the trouble of reloading the entire list.

1 Comment

In case anyone else comes along this way, this explanation is correct. However, one should note that the NotifyCollectionChanged event WILL NOT fire if an item is modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.