获取列表框windows phone 7的选定值

时间:2011-08-01 04:01:45

标签: c# windows-phone-7 isolatedstorage

我正在尝试从Windows Phone 7平台上的列表框中获取所选值。列表框中的数据由三列组成,包括2个文本块和1个图像对象。

我应该如何将代码放入我可以获取所选文本的文本(任何文本块中的数据)的方式?

以下是我定义网格的代码:

        //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);

    }

2 个答案:

答案 0 :(得分:1)

ListBoxItem.Content是您添加到ListBox.Items的网格。然后你可以访问Grid.Children来获得添加的TextBlocks,resp。他们的文字属性。

以上是正式答案。另一方面,尽管你的代码包含大量的空格,但我不认为它可以工作。例如,您要将多个图像(文本块)添加到单个网格单元格中。这是有意的吗?我不这么认为。你不想只使用一个日期(它是一个日期吗?),一个标题和一个图像?如果是这样,请改变你的逻辑。

答案 1 :(得分:0)

ben tan!

你可以得到控制标签:

示例:

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

当selectionChange你在控制网格中得到Tab?