views:

38

answers:

2

Hi, I have a class

public class Foo
    {
        public List<string> list1 { get; set;}
        public List<string> list2 { get; set; }  
        public string url;
    }

and a ListView with two columns

 <ListView Name="listview" ItemsSource="{Binding}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="list1" 
                       DisplayMemberBinding="{Binding Path=list1}" />
                <GridViewColumn Header="list2" 
                       DisplayMemberBinding="{Binding Path=list2}" />
            </GridView>
        </ListView.View>
    </ListView>

How i can to bind instance of Foo class to ListView?

Here i set a DataContext

listview.DataContext = new Foo()
                                       {
                                           list1 = new[] { "dsfasd", "asdfasdf", "asdf", "asdfsd" }.ToList(),
                                           list2 = new[] { "dsfasd", "asdfasdf", "asdf", "asdfasd" }.ToList()
                                       };

But it's not work.

A: 

I would set its datacontext in code behind to your instance of Foo.

Sdry
I already set datacontext but it still doesnt work
Neir0
Oke sorry, I looked over it a bit too fast. Could you clarify what you are precisely trying to do ?(the result you want) I think there is a better way to this then the current approach.
Sdry
A: 

Not sure what you are trying to do, but list1 and 2 are two collections. ItemsSource itself must be a collection (or possibly at least an Ienumerable, I don't know off my head). I don't see that your class Foo implements anything that resemblles a collection. You will not be able to bing to list1/2 that way.

You can e.g. let Foo implement IEnumerable<KeyValuePair<string,string>> and then yield out Key Value pairs with your strings. Then you could bind to Properties Key and Value.

flq