-2

This is the code behind, an array of People (ObservableCollection ) objects.

private ObservableCollection<People> data = new ObservableCollection<People>();

    public SecondWindow()
    {
        InitializeComponent();

        data.Add(new People() { Name = "JOhn Doe", Age="34" });
        data.Add(new People() { Name = "Jane Doe", Age = "45" });
        data.Add(new People() { Name = "Peter Singh", Age = "26" });

        this.DataContext = this;
    }



    public class People : INotifyPropertyChanged
    {
        private String name, age;
        public String Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        public String Age
        {
            get { return this.age; }
            set
            {
                if (this.age != value)
                {
                    this.age = value;
                    this.NotifyPropertyChanged("Age");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String prop)
        {
            if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

This is the XAML code, but it's not binding

<BlockUIContainer>
                <ListView BorderThickness="0" 
                          ItemsSource="{Binding Path=data}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn 
                                Header="Name" 
                                DisplayMemberBinding="{Binding Name}" 
                                Width="150" />
                            <GridViewColumn 
                                Header="Age" 
                                DisplayMemberBinding="{Binding Age}" 
                                Width="75" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </BlockUIContainer>

Any ideas?

D. Rattansingh
  • 1,569
  • 3
  • 19
  • 30
  • When you debug your application, take a look at the Output Window in Visual Studio. You will notice data binding error messages. – Clemens Jan 06 '22 at 16:15
  • System.Windows.Data Error: 40 : BindingExpression path error: 'data' property not found on 'object' ''SecondWindow' (Name='')'. BindingExpression:Path=data; DataItem='SecondWindow' (Name=''); target element is 'ListView' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') – D. Rattansingh Jan 06 '22 at 16:19

1 Answers1

1

The problem comes from using a field and not a public property for data:

public ObservableCollection<People> data { get; } = new ObservableCollection<People>();
Clemens
  • 123,504
  • 12
  • 155
  • 268
Jorge Machado
  • 202
  • 1
  • 3