2011-08-01 55 views
0

我想從Windows Phone 7平臺上的列表框中獲取選定的值。我的列表框中的數據由三列組成,包括2個文本塊和1個圖像對象。獲取列表框的選擇值Windows Phone 7

我應該如何把代碼放在我能得到所選文本(任何文本塊中的數據)的方式?

下面是我定義網格代碼:

 //Define grid column, size 

     Grid schedule = new Grid(); 

     foreach (var time in timeSplit) 
     { 
      timeList = time; 
      //Column 1 to hold the time of the schedule 
      ColumnDefinition scheduleTimeColumn = new ColumnDefinition(); 
      GridLength timeGrid = new GridLength(110); 
      scheduleTimeColumn.Width = timeGrid; 
      schedule.ColumnDefinitions.Add(scheduleTimeColumn); 

      //Text block that show the time of the schedule 
      TextBlock timeTxtBlock = new TextBlock(); 
      timeTxtBlock.Text = time; 
      //Set the alarm label text block properties - margin, fontsize 
      timeTxtBlock.FontSize = 28; 
      timeTxtBlock.Margin = new Thickness(0, 20, 0, 0); 
      //Set the column that will hold the time of the schedule 
      Grid.SetColumn(timeTxtBlock, 0); 

      schedule.Children.Add(timeTxtBlock); 
     } 

     foreach (var title in titleSplit) 
     { 
      titleList = title; 

      //Column 2 to hold the title of the schedule 
      ColumnDefinition scheduleTitleColumn = new ColumnDefinition(); 
      GridLength titleGrid = new GridLength(500); 
      scheduleTitleColumn.Width = titleGrid; 
      schedule.ColumnDefinitions.Add(scheduleTitleColumn); 

      //Text block that show the title of the schedule 
      TextBlock titleTxtBlock = new TextBlock(); 

      if (title.Length > 10) 
      { 
       string strTitle = title.Substring(0, 10) + "...."; 
       titleTxtBlock.Text = strTitle; 
      } 
      else 
      { 
       titleTxtBlock.Text = title; 
      } 

      //Set the alarm label text block properties - margin, fontsize 
      titleTxtBlock.FontSize = 28; 
      titleTxtBlock.Margin = new Thickness(60, 20, 0, 0); 
      //Set the column that will hold the title of the schedule 
      Grid.SetColumn(titleTxtBlock, 1); 

      schedule.Children.Add(titleTxtBlock); 
      //scheduleListBox.Items.Add(schedule); 
     } 

     foreach (var category in categorySplit) 
     { 
      categoryList = category; 

      //Column 3 to hold the image category of the schedule 
      ColumnDefinition categoryImageColumn = new ColumnDefinition(); 
      GridLength catImgnGrid = new GridLength(70); 
      categoryImageColumn.Width = catImgnGrid; 
      schedule.ColumnDefinitions.Add(categoryImageColumn); 

      TextBlock categoryTxtBlock = new TextBlock(); 
      categoryTxtBlock.Text = category; 

      //set the category image and its properties - margin, width, height, name, background, font size 
      Image categoryImage = new Image(); 
      categoryImage.Margin = new Thickness(-50, 15, 0, 0); 
      categoryImage.Width = 50; 
      categoryImage.Height = 50; 
      if (category == "Priority") 
      { 
       categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative)); 
      } 
      else 
       if (category == "Favourite") 
       { 
        categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative)); 
       } 


      Grid.SetColumn(categoryImage, 2); 
      schedule.Children.Add(categoryImage); 
     } 

     scheduleListBox.Items.Add(schedule); 
    } 

代碼列表框的設定值:

string selectedName; 

    private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e) 
    { 
     //Get the value of selected value in scheduleListBox 

     if (null != scheduleListBox.SelectedItem) 
     { 
      selectedName = (scheduleListBox.SelectedItem as ListBoxItem).Content.ToString(); 
     } 
     MessageBox.Show("Selected name : " + selectedName); 

    } 

回答

0

本曬黑!

你可以得到控制的標籤:

例子:

string a = "abc" 
grid myGrid = new grid(); 
myGrid.Tag = a; 

時selectionChange你在控制電網獲得標籤?

1

ListBoxItem.Content是您添加到ListBox.Items的網格。然後,您可以訪問Grid.Children以獲取添加的TextBlocks,他們的文本屬性。

以上是正式答案。在另一個說明中,儘管你的代碼包含大量的空白,但我不認爲它可以工作。例如,您要將多個圖像(文本塊)添加到單個網格單元格中。這是打算嗎?我不這麼認爲。你不想用listbox itm只有一個日期(是日期嗎?),一個標題和一個圖像?如果是這樣,改變你的邏輯。

相關問題