2012-05-24 21 views
1

我試圖從Linq創建單個選擇列表框,但是,我遇到了問題,設置每個列表框項目的id值與相應的ID保存在一個本地數據庫。我有以下代碼:在Windows Mobile 7中設置ListBoxItem的id值

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] 
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代碼:

<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 > 

現在我試圖重新編碼與MrMDavidson的建議的工作,但是,使用這個當前的代碼,它會在列表中顯示「AppName.Tips」而不是實際的列數據。我已經嘗試了谷歌搜索演示和教程,但是,我一直無法提出解決方案。

此外,是否有一種特殊的方式來處理日期和時間?

任何建議,將不勝感激。

+0

http://forums.asp.net/t/1468638.aspx/1 –

回答

4

這聽起來像你正在嘗試從WinForms/CF.Net風格的方法做到這一點。雖然你可能會這樣做,但我會建議你不要這樣做,因爲你真的沒有獲得Silverlight的價值。假設你的Tips類看起來像這樣;

public class Tip { 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public string Restaurant { get; set; } 
} 

然後,您可以列出這些綁定到ListBox;

listBoxTipHistory.ItemsSource = get_Tips() 

您的XAML用於描述如何顯示這些項目;

<ListBox x:Name="listBoxTipHistory"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <TextBlock Text="{Binding Title}" /> 
     <TextBlock Text="{Binding Restaurant}" /> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

ListBoxSelectedItem將當前選中的Tip(作爲object),而不是選中後實際ListBoxItem。所以要得到所選Tip的ID;

Tip selectedTip = listBoxTipHistory.SelectedItem as Tip; 
if (selectedTip != null) { 
    Console.WriteLine("The selected Tip is {0}", selectedTip.Id); 
} 

另一項建議是爲你尋找到了INotifyPropertyChanged界面,將允許更改模型自動獲取在UI

+0

我已經嘗試了這種方法,但是我列出的結果是列出的每個項目的「AppName.Tips」。其中一個項目被標記爲「resturant」,因此我創建了「」,但它不顯示預期結果。有什麼建議麼?此外,如何在一個文本塊中顯示多個項目,如「resturant,title,id」? – Euthyphro

+0

我已經更新了包含多個綁定屬性的答案。你能否澄清你的意思是「標籤餐廳」? – MrMDavidson

+0

我已更新我的問題,以更好地反映您的建議和我卡住的部分。 – Euthyphro

0

我跟你是從接近這一@MrMDavidson同意更新老派的觀點,我也同意使用ViewModel/Binding方法會更好,但我會給出一個答案,不要求您重寫迄今爲止所做的工作。

設置DataContextListItem到尖端對象:DataContext

foreach (Tips tip in tips) 
{ 
    ListBoxItem li1 = new ListBoxItem(); 
    li1.Content = tip.date.Day + "-" // etc  
    li1.DataContext = tip; 

    listBoxTipHistory.Items.Add(li1); 
} 

想作爲其中包含一個對象的控制(ListItem)的後面的容器。理想情況下,您將有ListItem從其DataContext中檢索數據並填充其內容,但那是另一天。現在只需將它想象爲一個容器,您可以使用它來存儲與該控件相關的數據。

現在,當你選擇一個項目或用戶按提交,就可以輕鬆檢索項:

foreach (var item in listBoxTipHistory.SelectedItems) 
{ 
    var tip = (Tip)item.DataContext; 
    var tipId = tip.tip_ID; // or whatever you need to do 
}