简体   繁体   中英

How to refresh a listview control after editting an item?

I started playing with the wpf listview control. I've created an "add" button and an "edit" button. The "add" button works as expected - whenever I'm adding a new item to the list it gets displayed. My problem is with the "edit" button - what is the correct way of informing the listView control that an item was changed? (it works in the attached code, i just wanted to know if there is a better way)

This is my code:

Xaml:

<Window x:Class="WpfApplication5.MainWindow" Name="This"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    >
<Grid>
    <ListView   Name="Mylist"
                ItemsSource= "{Binding ElementName=This, Path=People}"
                SelectionMode="Single"
                >
        <ListView.View>
                <GridView AllowsColumnReorder="false">
                    <GridViewColumn 
                            Header="Name" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn 
                            Header="Id" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Id}" />
                </GridView>
            </ListView.View>
    </ListView>
    <StackPanel Orientation="Horizontal" Height="45" Margin="190,133,197,133">
        <Button 
                    Content="Add"
                    Click="AddButton_Click"
                    />
        <Button 
                    Content="Edit"
                    Click="EditButton_Click"
                    />
    </StackPanel>
</Grid>
</Window>

Code Behind:

namespace WpfApplication5
{
public class PersonalDetails
{
    public string Name {get; set;}
    public string Id {get; set;}
}

public partial class MainWindow : Window
{
    private ObservableCollection<PersonalDetails> people = new ObservableCollection<PersonalDetails>();
    public ObservableCollection<PersonalDetails> People
    {
        get { return this.people; }
    }

    public MainWindow()
    {
        PersonalDetails p1 = new PersonalDetails();
        p1.Name = "Jeff";
        p1.Id = "111";
        people.Add(p1);
        InitializeComponent();
    }

    private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails p2 = new PersonalDetails();
        p2.Name = "Tom";
        p2.Id = "222";
        people.Add(p2);
    }

    private void EditButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails pItem = (PersonalDetails)Mylist.SelectedItem;
        if (pItem == null)
        {
            return;
        }
        pItem.Name = "Dan";
        Mylist.Items.Refresh();

    }

}
}

Your PersonalDetails class should implement the INotifyPropertyChanged interface.

Then when the Name property changes and you raise the PropertyChanged event the WPF binding infrastructure will respond by refreshing the presentation.

XAML:

<ListView Name="listResult"  ItemsSource="{Binding ItemsCollection}"></ListView>

Code Behind:

private ObservableCollection<object> itemsCollection = new ObservableCollection<object>();

public ObservableCollection<object> ItemsCollection
{
    get { return this.itemsCollection; }
}


private void UpdateSectionsList()
{
    List<object> tempList = ... //Put your element here

    // clear the list before and reload the object you want to display
    itemsCollection.Clear();

    if (tempList.Count > 0)
    {
        foreach (object item in tempList)
        {
            itemsCollection.Add(item);
        }
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM