关于c#:获取列表框windows phone 7的选定值

Getting selected value of listbox windows phone 7

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        //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);
    }

列表框选定值的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
    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);

    }

ListBoxItem.Content 是您添加到 ListBox.Items 的 Grid。然后,您可以访问 Grid.Children 以分别添加 TextBlocks。他们的 Text 属性。

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


本谭!

您可以获得控制选项卡:

示例:

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

当 selectionChange 你在控件 Grid 中获得 Tab ?