2011-08-01 57 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只有一个日期(是日期吗?),一个标题和一个图像?如果是这样,改变你的逻辑。

相关问题