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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:30:44+00:00 2026-05-13T05:30:44+00:00

OK, this must be a duplicate question, but I cannot find the answer: I

  • 0

OK, this must be a duplicate question, but I cannot find the answer:
I have a listbox that is databound to a Collection. By default Items[0] is selected. How can I prevent this in order to ensure that the SelectionChanged event is raised any time I click a ListBoxItem?

EDIT:
I had already dismissed the SelectedIndex=-1 route, but I tried again: Setting the SelectedIndex to -1 in the Listbox’s constructor (or as an attribute in XAML) does not work. It seems that the listbox is populated after the initialization and the selectedindex will become 0 after all.Same story for setting the SelectedItem to null;

I tried this:

<ListBox ItemsSource="{Binding Value}" 
         SelectionChanged="ListBox_SelectionChanged"
         IsSynchronizedWithCurrentItem="True"
         Loaded="ListBox_Loaded">
</ListBox>

with:

 private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems.Count==0)return;//Prevents stackoverflow! ;-)
        ((ListBox)e.OriginalSource).SelectedItem=null;
    }

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
    ((ListBox) sender).SelectedItem = null;
}

This works, BUT it inserts a blank line on top of the items that the listbox displays, which is very ugly….
In my particular case I could solve the problem by removing the IsSynchronizedWithCurrentItem attribute.

But I can think of many scenarios in which this would not be acceptable.

The above statement is nonsense: either you want to make use of master-detail binding and set the IsSynchronizedWithCurrentItem to true, or you don’t. It is rather unlikely that you want to make use of master-detail binding and then have no currently selected item in your listbox all the time

  • 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-13T05:30:44+00:00Added an answer on May 13, 2026 at 5:30 am

    Use CollectionView as an ItemsSource for your list box. A fresh WPF project with no item initially selected:

    XAML:

    <Window x:Class="ListBoxDataBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox 
            ItemsSource="{Binding Path=Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
            SelectionChanged="ListBox_SelectionChanged"
            IsSynchronizedWithCurrentItem="True"
            >
        </ListBox>
    </Grid>
    </Window>
    

    cs:

    public partial class Window1: Window {
        public IEnumerable Value {
            get { return (IEnumerable)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
    
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(IEnumerable), typeof(Window1), new UIPropertyMetadata(null));
    
    
        public Window1() {
            InitializeComponent();
            var view = new TheCollectionView();
            view.MoveCurrentTo(null);
            this.Value = view;
        }
    
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            MessageBox.Show("Selection changed");
        }
    }
    
    public class TheCollectionView: CollectionView {
        public TheCollectionView(): base(new TheCollection()) {
        }
    }
    
    public class TheCollection: List<string> {
        public TheCollection() {
            Add("string1");
            Add("string2");
            Add("string3");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This must be a very simple question, but I don't seem to be able
I think this must be simple but I can't get it right... I have
I'm sure this must be possible, but I can't find out how to do
I think this must be a stupid question, but why do the results of
This must be a classic .NET question for anyone migrating from Java. .NET does
Now this must be easy, but how can sum two NSNumber ? Is like:
I'm sure this must be a common problem. I've got a class that in
This must be a common problem because I see it in many published papers
How do I lock compiled Java classes to prevent decompilation? I know this must
What is the best way to change a user-password remotely in Unix? This must

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.