Gleb
Gleb

Reputation: 1432

Wpf binding custom class with list to ListBox

I'm new in WPF, and trying to work with binding. Now I'm binding a list of custom object to ListBox like this:

public class Foo
{
    public string Name { get; set; }
}

This is my C# code:

public List<Foo> lst = new List<Foo>();

public MainWindow()
{
    Bar bar = new Bar();

    lst.Add(new Foo { Name = "111" });
    lst.Add(new Foo { Name = "222" });
    lst.Add(new Foo { Name = "333" });
    lst.Add(new Foo { Name = "444" });
    lst.Add(new Foo { Name = "555" });     

    this.DataContext = lst;
    InitializeComponent();
}

And also XAML:

<ListBox Height="139" HorizontalAlignment="Left" Margin="44,102,0,0" Name="listBox1" VerticalAlignment="Top" Width="350" 
             ItemsSource="{Binding}">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Name}"></TextBlock>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

That's work fine but I want to bind a class that contains List as field, so my C# code looks like:

public class Foo
{
    public string Name { get; set; }
}

public class Bar
{
    private List<Foo> lst;

    public List<Foo> Lst
    {
        get;
        set;
    }

    public Bar()
    {
        lst = new List<Foo>();
        lst.Add(new Foo { Name = "111" });
        lst.Add(new Foo { Name = "222" });
        lst.Add(new Foo { Name = "333" });
        lst.Add(new Foo { Name = "444" });
        lst.Add(new Foo { Name = "555" });            
    }
}

And also

Bar bar=new Bar();
this.DataContext=bar;

When I doing so, nothing works. So I can't figure out how can I bind bar.Lst to ListBox. Can anybody explain me?

Upvotes: 1

Views: 7390

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39956

You must use Lst property not lst field. it is better using AutoProperty And then your DataContext like this:this.DataContext = bar.Lst; So Change your code in this way:

public class Bar
{
    public List<Foo> Lst { get; set; }

    public Bar()
    {
        Lst = new List<Foo>();
        Lst.Add(new Foo { Name = "111" });
        Lst.Add(new Foo { Name = "222" });
        Lst.Add(new Foo { Name = "333" });
        Lst.Add(new Foo { Name = "444" });
        Lst.Add(new Foo { Name = "555" });
    }
}

And:

public MainWindow()
{
     InitializeComponent();
     Bar bar = new Bar();
     this.DataContext = bar.Lst;
}

Upvotes: 2

Hari Prasad
Hari Prasad

Reputation: 16956

You have to make little modification to your Bar class.

public class Bar
{
    private List<Foo> lst;

    public List<Foo> Lst
    {
        get { return lst;}        
    }

    public Bar()
    {
        lst = new List<Foo>();
        lst.Add(new Foo { Name = "111" });
        lst.Add(new Foo { Name = "222" });
        lst.Add(new Foo { Name = "333" });
        lst.Add(new Foo { Name = "444" });
        lst.Add(new Foo { Name = "555" });            
    }
}

And in you Xaml.

<ListBox Height="139" HorizontalAlignment="Left" Margin="44,102,0,0" Name="listBox1" VerticalAlignment="Top" Width="350" 
         ItemsSource="{Binding Lst}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 4

Related Questions