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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:15:49+00:00 2026-05-10T19:15:49+00:00

I have this ListBox which is bound to an ObservableCollection. Each object in the

  • 0

I have this ListBox which is bound to an ObservableCollection. Each object in the list implements an interface called ISelectable

public interface ISelectable : INotifyPropertyChanged {     event EventHandler IsSelected;     bool Selected { get; set; }     string DisplayText { get; } } 

I want to keep track of which object is selected regardless of how it is selected. The user could click on the representation of the object in the ListBox but it could also be that an object is selected via code. If the user selects an object via the ListBox I cast the the selected item to an ISelectable and set the Selected property to true.

ISelectable selectable = (ISelectable)e.AddedItems[0]; selectable.Selected = true; 

My problem is that when I select the object using code I can’t get ListBox to change the selected item. I’m using a DataTemplate to show the selected object in a different color which means everything is displayed correctly. But the ListBox has the last object the user clicked as the SelectedItem which means that item can’t be clicked without first selecting another object in the list.

Anyone got any idea on how to solve this? I pretty sure I can accomplish what I want by writing some custom code to handle the Mouse and Keyboard events but I rather not. I have tried adding a SelectedItem property to the collection and bind it to the ListBox’s SelectItemProperty but no luck.

  • 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. 2026-05-10T19:15:50+00:00Added an answer on May 10, 2026 at 7:15 pm

    You could also accomplish this by data binding ListBoxItem.IsSelected to your Selected property. The idea is to set the binding for each of the ListBoxItems as they are created. This can be done using a style that targets each of the ListBoxItems generated for the ListBox.

    This way when an item in the ListBox is selected/unselected, the corresponding Selected property will be updated. Likewise setting the Selected property in code will be reflected in the ListBox

    For this to work the Selected property must raise the PropertyChanged event.

    <List.Resources>     <Style TargetType='ListBoxItem'>         <Setter              Property='IsSelected'              Value='{Binding                          Path=DataContext.Selected,                          RelativeSource={RelativeSource Self}}'              />     </Style> </List.Resources> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox which is bound to ObservableCollection of LogMessages. public ObservableCollection<LogMessage> LogMessages
I have a ListBox to which I bound a sorted list of strings. I
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a listbox which is bound to a CollectionViewSource (backed by an ObservableCollection).
I have a WPF ObservableCollection which is bound to a ListBox and I have
I have Listbox which is bound to my view model. This view model has
[Original] I have a ListBox which has its ItemsSource (this is done in the
I have a Listbox DataTemplate which looks like this <DataTemplate> <StackPanel Margin=0,24,0,24> <StackPanel toolkit:TiltEffect.IsTiltEnabled=true>
I have this problem when i try to synchronize a observable list with listbox/view
I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo

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.