如何从列表框项目访问类?
我构建了一个存储 XML 文件中的值的类。我将该数据加载到一个列表框中,如下所示:
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#。
I build a class that stores values from an XML file. I am loading that data into a ListBox like this:
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);
When I press and hold an item in the ListBox, I am calling a function that brings up context menu like this:
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();
}
}
}
So my question is how can I access the data from the Books class that is stored into each of the ListBoxItems? I am using C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是这个意思吗?
书籍 book = theBookListBox.SelectedItem as Books;
is that what you mean?
Books book = theBookListBox.SelectedItem as Books;
我认为你的课程在表格内。
所以最好这样做......
然后......
I think your class is inside of the form.
So its better do this...
then.....