DataBinding to ListView
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>
开发者_如何学Go<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.
I would set its datacontext in code behind to your instance of Foo.
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.
精彩评论