如何将数据绑定到wp7中的列表框 [英] how to bind data to listbox in wp7

查看:71
本文介绍了如何将数据绑定到wp7中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据绑定到wp7中的列表框

i am binding data to listbox in wp7

这是代码

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

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

//类函数

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

//编码

        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. 在进行绑定之前,请确保已调用budgetcategorywise()
  2. 请将绑定更改为:

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

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

第二次更改的原因是您的代码在Linq列表生成中使用了ToString()-这意味着带有category字段的类将在字符串表示形式中展平.

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

这篇关于如何将数据绑定到wp7中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆