简体   繁体   中英

how to bind data to listbox in wp7

i am binding data to listbox in wp7

here is the code

              <ListBox x:Name="list_budget" Width="440">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Name="txtname" Text="{Binding category}"></TextBlock>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

//class function

    public string[] jinal;

    public void  budgetcategorywise()
    {

        var q = from shoppingItem p in db.Item1
                group p by new { p.category_name } into g
                select new { category = g.Key, total = g.Sum(p => p.total_amt) `enter code here`}.ToString();

      jinal = q.toarray();
}

//coding

        list_budget.ItemsSource = App.Viewmod.jinal;

now,the error is query is ok result is perfact but i am not able to bind the data to listbox.

Looking at your code sample:

  1. Please make sure budgetcategorywise() is called before you do the binding
  2. Please change your binding to:

      <TextBlock Name="txtname" Text="{Binding}"></TextBlock> 

The reason for this second change is that your code uses a ToString() in the Linq list generation - which means that the class with its category field is flattened in a string represenation.


If you wish to keep the category field in your binding then use a class for your list items like:

   public class MyListItem
   {
       public string category { get;set; }
       public double total { get;set; }
   }

   public List<MyListItem> jinal;

   public void  budgetcategorywise()
   {

        var q = from shoppingItem p in db.Item1
                group p by new { p.category_name } into g
                select new MyListItem() { category = g.Key, total = g.Sum(p => p.total_amt) };

      jinal = q.ToList();
   }

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