how to bind data to listbox in wp7

余生颓废 提交于 2019-12-13 04:45:13

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/10880429/how-to-bind-data-to-listbox-in-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!