Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:52:54+00:00 2026-05-29T07:52:54+00:00

I have a listbox that binds to and displays the Name elements from an

  • 0

I have a listbox that binds to and displays the Name elements from an XML file. When a listbox item is selected, I want to display the Price value associated with this item in a textblock. How do I retrieve the Price programmatically (meaning not in the xaml file but in code behind)? Thanks.

XML file has these nodes:

<Product>
    <Name>Book</Name>
    <Price>7</Price>
</Product>

I use Linq and do the select with an anonymous type. If the easiest way to access the field programmatically is through a named type, please show me how.

Here’s how I bind in xaml (using a data template for each listbox item that contains):

 <TextBlock Text = "{Binding Name}" />

Here’s the code-behind function where I want to retrieve the Price:

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    { 
       // how do I get the value of Price of the selected item here?
    }

Please note that I want to access Price in this function, NOT in xaml!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:52:55+00:00Added an answer on May 29, 2026 at 7:52 am

    First of all you probably don’t even need LINQ as you can do a lot of things with XmlDocuments including doing selection via XPath (also in Bindings).

    Secondly converting anonymous types to named types is trivial, if you have

    select new { Name = ..., Price = ... }
    

    You just need a class with the respective properties

    select new Product { Name = ..., Price = ... }
    
    public class Product
    {
         public string Name { get; set; }
         public string Price { get; set; } // Datatype is up to you...
    }
    

    Thirdly you can make do without named types using dynamic.

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    { 
       var listBox = (ListBox)sender;
       // Named type:
       Product item = (Product)listBox.SelectedItem;
       // Anonymous type:
       dynamic item = listBox.SelectedItem;
       // <Do something with item.Price, may need to cast it when using dynamic>
       // e.g. MessageBox.Show((string)item.Price);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a listbox that has its selected item bound to a property
I have an XML file with these nodes: <Product> <Name>... <Color>... <Price>... </Product> I
I have a user control that inherits from the ListBox class and displays a
I have listbox that displays information about list of objects: <ListBox Grid.Column=0 Height=152 Name=CustomersList
I have a ListBox in my wpf window that binds to an ObervableCollection .
I have a ListBox that when in focus, and when I have an item
I have a ListBox that is bound to a list of Persons. I want
I have a ListBox that displays a number of usercontrols that are bound to
I have a listbox that displays Shipment Items (custom class) that are formatted using
I'm writing a Silverlight application. I want a Listbox that displays the rows vertically.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.