2012-07-03 4 views
0

내가하려는 것은 목록 상자에서 선택한 항목의 속성 값을 가져 오는 것입니다.선택한 목록 상자 항목의 속성 값을 얻는 방법

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    //listBox1.Items.Clear(); 

    IList<FoodViewModel> food = this.Getfoodlist();  
    List<Foodlist> foodItems = new List<Foodlist>(); 

    foreach (FoodViewModel foodlist in food) 
    { 
     int foodID = foodlist.C_ID; 
     string foodDetail = foodlist.FoodDetail; 
     string foodTime = foodlist.FoodTime; 
     string foodDate = foodlist.DateofFood; 

     foodItems.Add(new Foodlist() { C_ID = foodID, FoodTime = foodTime, DateofFood = foodDate, FoodDetail = foodDetail}); 
    } 

    listBox1.ItemsSource = foodItems; 
} 

public class Foodlist 
{ 
    public int C_ID { get; set; } 
    public string DateofFood{ get; set;}  
    public string FoodTime{ get; set;}  
    public string FoodDetail{ get; set;} 
} 

XAML 코드 -이

<ListBox Height="528" HorizontalAlignment="Left" Margin="1,4,0,0" Name="listBox1" VerticalAlignment="Top" Width="453"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Name="foodDetail" 
          Text="{Binding FoodDetail}" /> 
       <TextBlock Name="date" 
          Text="{Binding DateofFood}" /> 
       <TextBlock Name="time" 
          Text="{Binding FoodTime}" /> 
       <TextBlock Name="ID" 
          Text="{Binding C_ID}" Visibility="Collapsed" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

지금, 내가 얻고 자하는 것은 선택 목록 상자 항목의 C_ID (값). 어떤 제안?

답변

2

그것은이 같은 것입니다 :

private void listBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
{ 
    if (e.AddedItems.Count > 0) 
    { 
     var c_id = (e.AddedItems[0] as Foodlist).C_ID; 
    } 
} 

Page_Ctor --> listBox1.SelectionChanged += listBox1_SelectionChanged; 

환호, 나는이 "Page_Ctor 배치합니까

+0

그것은 오류 '이름'E '현재 컨텍스트에 존재하지 않는 "를 제공합니다. –

+0

어디서? 런타임에? 그것을 구축? –

+1

:)이 오류를 해결했습니다. 어디서 이것을 배치해야합니까? "Page_Ctor -> listBox1.SelectionChanged + = listBox1_SelectionChanged;" –

0

-> listBox1.SelectionChanged + = listBox1_SelectionChanged ; " 여기

는 :

<ListBox .... SelectionChanged="listBox1_SelectionChanged"> 
+0

감사합니다. 완벽하게 작동 :) –

관련 문제