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 8889139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:11:21+00:00 2026-06-14T22:11:21+00:00

What I want to do: I have a ListBox bound to an ObservableCollection<MyClass> .

  • 0

What I want to do:
I have a ListBox bound to an ObservableCollection<MyClass>. When I select an item, I want to display some of its properties in some labels. What are the steps here? I have set the content of labels to {Binding Path=PropertyName}. What should be their DataContext?

How I was going do it: I’m keeping a private variable in my window’s code-behind called MyClass selectedItem, and I would like all bindings to point to that variable and its properties. It just seems more “semantic” to me, to bind to a variable with a clear meaning within the code logic, than to a presentation element’s selected item.

This variable can, and will, reference a different MyClass instance at any given time (I’ll change it in ListBox SelectionChanged handler). I want the bindings to reflect that – to always point to the properties of the instance currently referenced by selectedItem. Will this just work? Will the binding always point to the object that the variable points to? Or will it point to the object the variable is referencing at the moment I set up the binding? (the latter would be my guess)

Possible alternatives I can think of: Bind to listbox’s SelectedItem instead of the variable referencing it. If this is the way to go, how to make it type-safe? How to ensure the binding will know it’s not working with a generic ListBoxItem, but a MyClass instance, so it can see its properties? Is this needed?

Any suggestions, or explanations why I’m being stupid, or alternative approaches from the more experienced? Thanks!

  • 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-06-14T22:11:23+00:00Added an answer on June 14, 2026 at 10:11 pm

    I would recommend using your alternate approach, I think this is the common way to do what you want. If you dont fill your ListBox in code-behind or in your XAML, the SelectedItem-property will hold the MyClass instance and not a ListBoxItem instance.

    Then you either take the SelectedItem property of your ListBox as DataContext of your labels or you create a property of Type MyClass in your ViewModel which you bind to the SelectedItem property (Mode=TwoWay). Then you can bind to its properties like so:

    <Label Text="{Binding Path=MySelectedItem.Text}"/>
    

    In this case the DataContext of your Labels has to be the ViewModel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ObservableCollection that's bound to a ListBox in WPF. I want the
I have a WPF ListBox with its ItemsSource bound to an ObservableCollection navigation property
I have a ListBox that is bound to a list of Persons. I want
well i have a listbox with some items inside. i want to detect a
I want a ListBox full of items. Although, each item should have a different
I have a multi-select <asp:listbox> and a button. I want to disable the button
In my project I have a WPF Listbox bound to an ObservableCollection. Every time
I have this ListBox which is bound to an ObservableCollection. Each object in the
Using MVVM style I have successfully bound an ObservableCollection<string> to a ListBox , showing
I have a listbox and I want its ItemsSource to be all the items

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.