2011-08-01 147 views
0

我试图获取列表框的选定行。列表框由3列组成。我正在试图获取1列中的文本。从列表框中选定的行列获取值(Windows Phone 7)

下面是我的代码,但我在SubItem[0]:

string home = 
    scheduleListBox.Items[selectedIndexOfSchedule].SubItem[0].ToString(); 

对象得到一个错误不包含Subitem定义,并没有扩展方法SubItem接受object类型的第一个参数可以发现(有您是否缺少使用指令或装配参考?

我该如何修改我的代码?


XAML列表框:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,0,0"> 
    <ListBox Name="scheduleListBox" 
      Height="570" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" Width="472" 
      ItemsSource="{Binding LibraryItems}" 
      SelectionChanged="scheduleListBox_SelectionChanged_1" /> 
    </Grid> 
</Grid> 

下面是我定义网格代码:

//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); 
} 
+0

这是正确的,系统会默认列表项没有'SubItem'情况下,因为你可能正在使用一个自定义的'DataTemplate'。什么是你的'ListBox'的XAML? –

回答

2

的问题是,scheduleListBox.Items[selectedIndexOfSchedule]回报你交流#System.Object,这不有一个SubItem属性。

你需要做的就是将这个System.Object转换为它的原始类,它是一个System.Windows.Controls.Grid对象。所以,如果你想获得在时间列中的文本中选定的表格和存储在home

var selectedGrid = scheduleListBox.Items[selectedIndexOfSchedule] as Grid; 
var selectedTimeTextBlock = selectedGrid.Children[0] as TextBlock; 
var home = selectedTimeTextBlock.Text; 
+0

该子项[0]仍然有错误 –

+0

[ListBoxItem](http://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.aspx)没有SubItem属性。你可以发布你用来声明这个ListBox的XAML吗? – fsong

+0

我编辑了我的帖子 –