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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:36:32+00:00 2026-05-17T20:36:32+00:00

I looked at several related answers, and determined that I can programmatically clear the

  • 0

I looked at several related answers, and determined that I can programmatically clear the selection by setting lstData.SelectedIndex = -1; However, when I do it right after setting the data context on startup, somehow it never works and gets set to select the first element in the list.

I also tried adding the setting to the XAML, and -1 is actually Visual Studio’s default value, though it isn’t actually in the XAML unless you set it. I.e.:

    <ListView Margin="6,6,6,203" 
        IsSynchronizedWithCurrentItem="True" 
        x:Name="lstData" 
        ItemsSource="{Binding}" 
        SelectionChanged="lstData_SelectionChanged" 
        HorizontalContentAlignment="Right"
        ItemContainerStyle="{StaticResource ItemContStyle}"  
        SelectedIndex="-1">

But this also has no effect.

Also, fascinatingly, if I put lstData.SelectedIndex = 3; in my LoadData method, it WILL start set with the third member selected.

This is my relevant window loading code:

    public Window1()
    {
        InitializeComponent(); 

        // Set start and end dates to day after tomorrow, and 
        // the next day, by default:
        StartDate = DateTime.Now.AddDays(1);
        EndDate = StartDate.AddDays(2);
        txtStartDate.Text = StartDate.ToShortDateString();
        txtEndDate.Text = EndDate.ToShortDateString();
        LoadData();
    }

    public void LoadData()
    {
        App.RefreshMembers();

        App.CalculateNeededMeals(StartDate, EndDate);

        // Bind the ListBox to our ObserveableCollection
        LayoutRoot.DataContext = 
          App.db.PFW_Members.OrderBy("FullName",true).OrderBy("CancelDate",true);
        lstData.SelectedIndex = -1;
    }

And LoadData() gets called in other circumstances, and in that case it DOES clear the selection. Just not the first time the window loads. As if, there is some initialization thread that is not really finished and goes and sets the selection to 0 if it is -1 on startup.

Oh and yes I do have a selection changed handler, but it doesn’t change the selection, i.e.:

    private void lstData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (lstData.SelectedItem == null)
        {
            btnReactivate.IsEnabled = false;
            btnDeactivate.IsEnabled = false;
        }
        else
        {
            if (((PFW_Member)lstData.SelectedItem).CancelDate == null)
            {
                btnReactivate.IsEnabled = false;
                btnDeactivate.IsEnabled = true;
            }
            else
            {
                btnReactivate.IsEnabled = true;
                btnDeactivate.IsEnabled = false;
            }
        }
    }

Is there some way to make it really wait until loaded, and then do stuff, or to set a delayed event or something, or does someone have knowledge or a theory about what might be going on here?

Thanks for any tips!

  • 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-17T20:36:33+00:00Added an answer on May 17, 2026 at 8:36 pm

    Just set IsSynchronizedWithCurrentItem=”False”. This will be the easiest way to fix it.

    What’s happening here is this: after everything gets Initialized (i.e. after the constructor), the Bindings will kick in. The ListBox’s ItemsSource will be set at this time. During this step, some ICollectionView magic happens behind the scenes. Essentially, the ListBox will get connected to the source collection’s default CollectionView, whose CurrentItem property always defaults to the first item. Now, if IsSynchronizedWithCurrentItem is true, the ListBox will update the SelectedItem to be equal to the ICollectionView.CurrentItem. This is what’s causing the issue you mentioned.

    (Note: after all of these happens, the Loaded event will get fired. So, dnr3’s comment of setting the SelectedIndex=-1 in the Loaded event should also work).

    Hope this makes sense.

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

Sidebar

Related Questions

I've looked at several other SGen-related questions on here and either their answers don't
I've looked through several posts but I haven't quite found any answers that have
I know that there are several very similarly-related questions on this website, however after
I've looked through several other posts similar to this but can't find anything that
I've looked at several questions here that looked similar, but none of them discussed
I have looked through several answers for loading CSS pages. http://docs.djangoproject.com/en/dev/howto/static-files/ Basically, I believe
We're after an ORM/Domain Modelling tool that will allow us to generate several related
I've looked at several approaches to enterprise architecture for databases that store massive amounts
I have looked at several tutorials and different posts and can't seem to figure
I've looked at several other questions related, one answer even seemed like it solved

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.