C# 如何从列表框项访问类?
我构建了一个存储XML文件中的值的类。我正在将该数据加载到一个列表框中,如下所示:C# 如何从列表框项访问类?,c#,xaml,windows-phone-7,silverlight-4.0,C#,Xaml,Windows Phone 7,Silverlight 4.0,我构建了一个存储XML文件中的值的类。我正在将该数据加载到一个列表框中,如下所示: private void LoadBookXML(string bookXML, ListBox theBookListBox) { XDocument loadedData = XDocument.Load(bookXML); var data = from query in loadedData.Descendants("book")
private void LoadBookXML(string bookXML, ListBox theBookListBox)
{
XDocument loadedData = XDocument.Load(bookXML);
var data = from query in loadedData.Descendants("book")
select new Books
{
Title = (string)query.Attribute("title").Value.ToUpper(),
Theme = (string)query.Attribute("theme").Value,
Abbreviation = (string)query.Attribute("abbr").Value,
Chapters = (string)query.Attribute("chapters").Value,
Writer = (string)query.Attribute("writer").Value,
Place = (string)query.Attribute("place").Value,
Completed = (string)query.Attribute("completed").Value,
Time = (string)query.Attribute("time").Value,
Summary = (string)query.Attribute("summary").Value,
Link = (string)query.Attribute("link").Value,
Number = (string)query.Attribute("number").Value,
WriterAndPlace = "Written by " + (string)query.Attribute("writer").Value + " in " + (string)query.Attribute("place").Value,
};
theBookListBox.ItemsSource = data;
GestureService.GetGestureListener(theBookListBox).Hold += new EventHandler<GestureEventArgs>(theBookListBox_Hold);
private void theBookListBox_Hold(object sender, GestureEventArgs e)
{
AppState state = ThisApp._appState;
if (e.OriginalSource is TextBlock)
{
if (((string)((e.OriginalSource as TextBlock).Name)) == "btBookName")
{
UpdateLayout();
_cmReader = new ContextMenu();
MenuItem item = new MenuItem();
item.IsEnabled = true;
item.IsHitTestVisible = true;
TextBlock block = new TextBlock();
block.Text = "HELP, I AM TRYING TO GET DATA FROM THE BOOKS CLASS!!!";
block.TextDecorations = TextDecorations.Underline;
item.Header = block;
MenuItem item5 = new MenuItem
{
Header = "book info"
};
item5.Click += new RoutedEventHandler(bookInfo_Click);
_cmReader.Items.Add(item);
_cmReader.Items.Add(item5);
OpenContextMenu();
}
}
}
因此,我的问题是如何访问Books类中存储在每个ListBoxItems中的数据?我用的是C#。我想你的班级在表格里 所以最好这样做
private void LoadBookXML()
{
string bookXmL = "";
listBox theBookListBox = new ListBox();
Your other code here..
}
然后
private void theBookListBox_Hold(object sender, GestureEventArgs e)
{
AppState state = ThisApp._appState;
if (e.OriginalSource is TextBlock)
{
if (((string)((e.OriginalSource as TextBlock).Name)) == "btBookName")
{
LoadBookXML();
Your other code here...
}
}
}
这就是你的意思吗 Books book=图书列表框。选择EdItem作为图书
//All
foreach(Books book in theBookListBox.Items)
{
}
发送方的值是多少?另外,列表框的XAML可能会有所帮助,我想Geronimo会指出您想要的。当前选定项。