我试图获取列表框的选定行。列表框由3列组成。我试图在列中的一列中获取文本。
以下是我的代码,但我在SubItem[0]:
string home =
scheduleListBox.Items[selectedIndexOfSchedule].SubItem[0].ToString();
对象不包含
Subitem
的定义,并且没有可以找到接受类型SubItem
的第一个参数的扩展方法object
(您是否缺少using指令或装配参考?
我应该如何修改我的代码?
列表框的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 :(得分:2)
问题是scheduleListBox.Items[selectedIndexOfSchedule]
会返回一个c#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;