myoldgrandpa
myoldgrandpa

Reputation: 1019

GridView Binding doesn't work. What am I missing?

GridView Binding doesn't work. Could you check what I am missing ? I'm posting all my code for your convinience. The purpose of this test code is that if I click add button, person class will be added to the gridview.

C# code below...

public partial class MainWindow : Window
{
    List<Person> persons;
    public MainWindow()
    {
        InitializeComponent();
        persons = new List<Person>() { new Person() {Name="A", Age=20},
                                        new Person() {Name="B", Age=30}};

        lstView.ItemsSource = persons;
    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        persons.Add(new Person { Name = tbName.Text, Age = Int32.Parse(tbAge.Text) });
    }


}

public class Person : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private int age;
    public int Age
    {
        get { return age; }
        set 
        { 
            age = value;
            OnPropertyChanged("Age");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

And xaml code..

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Width="525" Height="150">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0">Name</TextBlock>
    <TextBox Grid.Column="1" x:Name="tbName"></TextBox>
    <TextBlock Grid.Column="0" Grid.Row="1">Age</TextBlock>
    <TextBox Grid.Column="1" Grid.Row="1" x:Name="tbAge"></TextBox>
    <Button Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Click="Add_Click"> Add </Button>
    <ListView Grid.Column="2" Grid.RowSpan="4" x:Name="lstView">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

Upvotes: 1

Views: 128

Answers (2)

Juan M. Elosegui
Juan M. Elosegui

Reputation: 6981

INotifyPropertyChanged is for changes on the object (in your case every Person instance).

You need an ObservableCollection (which implements INotifyCollectionChanged) to listen to changes in a collection (in your case new and deleted Person instances in the collection).

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

more info here

Upvotes: 1

thumbmunkeys
thumbmunkeys

Reputation: 20764

You need an ObservableCollection not a List

 ObservableCollection<Person> persons;

Upvotes: 1

Related Questions