2011-08-01 11 views
0

リストボックスの選択行を取得しようとしています。リストボックスは3列で構成されています。私は1つの列のテキストを取得しようとしています。以下は リストボックスの選択した行の列から値を取得する(Windows Phone 7)

は私のコードですが、私は(見つけることができる Subitemの定義と種類 objectの最初の引数を受け入れていない拡張メソッド SubItemが含まれていません SubItem[0]:

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

オブジェクトでエラーが出ます使用するディレクティブまたはアセンブリ参照がありません。

コードを変更する方法を教えてください。リストボックスの


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]戻りますのAC#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

投稿を編集しました –

関連する問題