如何将数据绑定到列表框 [英] how to bind data to listbox
本文介绍了如何将数据绑定到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将数据绑定到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 List<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) }.ToString();
jinal = q.ToList();
}
//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.
推荐答案
嗨.
首先在ListBox中添加ItemSource属性的定义.请参见以下内容:
Hi.
First of all add in ListBox definition of ItemSource property.See below:
<ListBox x:Name="list_budget" ItemSource="{Binding}" Width="440">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="txtname" Text="{Binding category}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
还有为什么将变量jinal声明为:
And alos why you declared variable jinal as:
List<string></string>
????
我想,它必须是
????
I suppose, that it must be
List<resultset> jinal</resultset>
,其中ResulttSet可以表示如下:
, where ResulttSet could be represent as follow:
public class ResulSet
{
public string Name {get; set;}
public int Total {get; set;}
}
另外,使用ObservableCollection而不是List会更受欢迎!
Also, it would be more prefarable to use ObservableCollection instead of List!!
这篇关于如何将数据绑定到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文