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?
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.