Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi after long time I started working in wpf, I am struggling with populating listbox through complete XAML binding below is my code.

Here is my collection class

C#
<pre>namespace WPFInterview
{
    public class Collection
    {
        public  IEnumerable<Person> _collection{get; set;}
        public Collection()
        { 
         FillObservableCollection();
        }

        private void FillObservableCollection()
        {
            _collection = new ObservableCollection<Person> { new Person { Name = "Ram", Age = 10 }, new Person { Name = "Shyam", Age = 11 }, new Person { Name = "Raju", Age = 13 } };
            
            // lstMy.ItemsSource = _collection;      
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }

        }
     
    }
}


Here is my XAML code with binding

<Window x:Class="WPFInterview.BindingElement"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
        xmlns:local="clr-namespace:WPFInterview"     
        Title="BindingElement" Height="300" Width="300">
    <Window.DataContext>
        <local:Collection/>
    </Window.DataContext>
    <Canvas>
        <ListBox Name="lstMy" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Canvas.Left="67" Canvas.Top="65" ItemsSource="{Binding _collection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Canvas>
</Window>


I am not able to poupulate listbox

What I have tried:

Yes I have tried but could not populate.
Posted
Updated 10-Jan-17 3:24am
Comments
[no name] 10-Jan-17 8:56am    
You need to implement INotifyPropertyChanged
rameshKumar1717 10-Jan-17 9:14am    
Above solution also works, I realized later.

Your collection needs to inherit from ObservableCollection:

C#
public ObservableCollection<MyObject> MyCollection { get; set; };

public MainWindow()
{
    this.InitializeComponents();
    this.DataContext = this;
    this.MyCollection = new ObservableCollection<MyObject>();
}


Then in your XAML, you can set ItemsSource="{Binding Path=MyCollection}".
 
Share this answer
 
v2
Comments
ridoy 10-Jan-17 11:11am    
a 5.
Well actually that populates both the collection and the list box, even at design time.

A bit simpler form
XML
<Window x:Class="WindowNameSpace.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SomeLocalNamespace"
        mc:Ignorable="d"
        Title="Window3" Height="300" Width="300">
    <Window.DataContext>
        <local:Collection/>
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding _collection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

And the class
C#
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SomeLocalNamespace {
   public class Collection {
      public IEnumerable<Person> _collection { get; set; }
      public Collection() {
         FillObservableCollection();
      }
      private void FillObservableCollection() {
         _collection = new ObservableCollection<Person> {
            new Person { Name = "Ram", Age = 10 },
            new Person { Name = "Shyam", Age = 11 },
            new Person { Name = "Raju", Age = 13 }
         };

      }
      public class Person {
         public string Name { get; set; }
         public int Age { get; set; }

      }
   }
}

If you open the form in design time or at run-time you see three rows containing the names and ages.

The reason is that you have defined the class in the data context of the Window. WPF then again instantiates these objects when used so this actually causes the collection to be created.

Then again you're populating the collection in constructor and the binding in the list box is referencing the collection you just populated so everything works.

However, as John Simmons pointed out, you should used observable collection so that the UI element reacts upon changes in collection. This also means that the class should implement INotifyPropertyChanged.
 
Share this answer
 
Comments
ridoy 10-Jan-17 11:11am    
a 5.
Wendelius 10-Jan-17 12:23pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900