1

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?

0

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.