开发者

Getting a value from a selected row column in list box (Windows Phone 7)

I trying to get the selected row of a list box. The listbox is made up of 3 columns. I am trying to get the text in 1 of the columns.

Below is my code but I get a error at SubItem[0]:

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

Object does not contain a definition of Subitem and no extension method SubItem accepti开发者_StackOverflow中文版ng a first argument of type object could be found (are you missing a using directive or an assembly reference?

How should I modify my code?


XAML of listbox:

<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>

Below is my code for defining the 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);
}


The problem is that scheduleListBox.Items[selectedIndexOfSchedule] returns you a c# System.Object, which does not have a SubItem property.

What you need to do is cast this System.Object to its original class, which is a System.Windows.Controls.Grid object. So if you want to get the text in the time column in the selected Grid and store that in home:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜