问题
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:
- Please make sure
budgetcategorywise()
is called before you do the binding 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