I’m trying to create a single select listbox from Linq, however, I’m running into problems setting the id value of each listbox item with the corresponding id as saved in a local database. I have the following code:
public void display_Tips()
{
//commented out to try MrMDavidson's approach.
// listBoxTipHistory.Items.Clear();
// IList<Tips> tips = this.get_Tips();
// foreach (Tips tip in tips)
// {
// ListBoxItem li1 = new ListBoxItem();
// li1.Content = tip.date.Day + "-" + tip.date.Month + "-" +
// tip.date.Year + "-" + tip.date.Hour + ":" +
// tip.date.Minute + " - " + tip.resturant + " - $" +
// tip.tip_amount + " - $" + tip.billTotal;
// listBoxTipHistory.Items.Add(li1);
// }
if (listBoxTipHistory.Items.Count > 0)
{
listBoxTipHistory.Items.Clear();
}
listBoxTipHistory.ItemsSource = get_Tips();
}
public IList<Tips> get_Tips()
{
IList<Tips> tips = null;
using (TipContext context = new TipContext(conString))
{
IQueryable<Tips> query =
(from c in context.TipTable
orderby c.date descending
select c);
tips = query.ToList();
}
return tips;
}
Table for local database:
[Table]
public class Tips
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int tip_ID
{
get;
set;
}
[Column(CanBeNull = false)]
public float tip_amount
{
get;
set;
}
[Column]
public string resturant
{
get;
set;
}
[Column(CanBeNull = false)]
public DateTime date
{
get;
set;
}
}
Xaml code:
<ListBox SelectionChanged="showTipDetails" x:Name="listBoxTipHistory" Margin="6,3,0,0">
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding resturant}" />
<TextBlock Text="{Binding date}" />
<TextBlock Text="{Binding tip_amount}" />
</StackPanel>
</DataTemplate>
</ListBox >
I’m now trying to recode to work with MrMDavidson’s suggestion, however, with this current code it results in the list showing “AppName.Tips” rather than the actual column data. I’ve tried googling walkthroughs and tutorials on this, however, I’ve been unable to come up with a solution.
Moreover, is there a special way to handle dates and time?
Any suggestions would be appreciated.
It sounds like you’re trying to do this from a WinForms / CF.Net style approach. Whilst you can probably do this I’d recommend against it as you’re really not getting the value of Silverlight. Let’s presume that your
Tipsclass looks something like;You can then bind a list of these to a
ListBox;Your
XAMLis used to describe how these items are displayed;The
SelectedItemon theListBoxwill be the currently selectedTip(as anobject), not the actualListBoxItemthat’s selected. So to get the ID of the selectedTip;A further suggestion would be for you to look into the INotifyPropertyChanged interface which will allow changes to your model to automatically get updated in the UI