简体   繁体   中英

bind listview with list of model class in xaml (win8)

I am working on metro app c#/xaml, I want to bind listview with webservice function which returns List<delNewsletter>

where delNewsletter is class

public class delNewsletter { public int Id { set; get; } public string name { set; get; } }

and I am binding like this :

lstNews.ItemsSource= await client.GetDeletedNewslettersAsync("token", 1, 2);

but listview item showing content like this test.win8.delNewsletter this is method path.

how i can bind listview?

You need to provide DisplayMemberPath to listView otherwise it will simply call ToString() on your object and will show class name which it is showing right now.

You can specify it either in code behind or in XAML -

lstNews.DisplayMemberPath = "name";

OR

<ListView DisplayMemberPath="{Binding name}"/>

But in case you want to show both Id and name in your listView, you need to provide template to your listView -

     <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Id}"/>
                <GridViewColumn DisplayMemberBinding="{Binding name}"/>
            </GridView>
        </ListView.View>
    </ListView>

could you try to override ToString method of your delNewsletter class. to something like that:

public override string ToString()
{
   return string.Format(CultureInfo.InvariantCulture, "Name: {0}, Id: {1}", Name, Id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM