如何从列表框项目访问类?

发布于 2024-12-04 05:48:03 字数 2698 浏览 1 评论 0原文

我构建了一个存储 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-12-11 05:48:03

你是这个意思吗?

书籍 book = theBookListBox.SelectedItem as Books;

//All
foreach(Books book in theBookListBox.Items)
{
}

is that what you mean?

Books book = theBookListBox.SelectedItem as Books;

//All
foreach(Books book in theBookListBox.Items)
{
}
淡看悲欢离合 2024-12-11 05:48:03

我认为你的课程在表格内。

所以最好这样做......

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...
        }
    }
}

I think your class is inside of the form.

So its better do this...

private void LoadBookXML()
{
  string bookXmL = "";
  listBox theBookListBox = new ListBox();

  Your other code here..
}

then.....

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...
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文