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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:26:30+00:00 2026-06-01T16:26:30+00:00

In the code behind of my Silverlight application, I have the need to re-populate

  • 0

In the code behind of my Silverlight application, I have the need to re-populate the TreeView and then make a specific TreeViewItem selected.

The code itself is pretty simple, here it is (i’ll trim and pseudo-code-ify it to make it as short as possible)

private void Button_Click()
{
    Guid idToSelect = TellMeWhatToSelect();
    List<myObject> myDataList = myObjectRepository.RetrieveData().ToList();

    myTreeView.Items.Clear();
    foreach(myObject o in myDataList)
    {
        myTreeView.Items.Add(new TreeViewItem() { Content = o.DataField, Tag = o.Id });
    }

    myTreeView.Items.First(o => ((Guid)(o as TreeViewItem).Tag).Equals(idToSelect)).IsSelected = true;

}

That’s basically it: i’m reading some data into myDataList, then i cycle through it and create as many TreeViewItems as needed in order to display the data.

Problem is, myTreeView.SelectedItem is null at the end of this, and SelectionChanged event isn’t triggered. I would think that, since Items collection has been cleared and re-filled, switching IsSelected on one of the items would act like clicking, but it seems it doesn’t).

Oddly enough (for me at least), issuing myTreeView.Items.First().IsSelected = true; by itself (that is, calling a method with that single line of code inside) works as expected: SelectedItem is there and all events are fired appropriateyl.

What’s wrong with my code and/or what am I missing ? Looks like cleaning items up kind of breaks something.

I’m fairly sure others have had similar issues, but a bunch of searches I tried didn’t help (most of the info & questions I came up with are WPF-related).

Thanks for your time, I’ll provide more info if needed. Also, sorry for the wall of text.

UPDATE

Modifying code like this, now the method works as expected.

private void Button_Click()
{
    Guid idToSelect = TellMeWhatToSelect();
    List<myObject> myDataList = myObjectRepository.RetrieveData().ToList();

    myTreeView.Items.Clear();
    foreach(myObject o in myDataList)
    {
        myTreeView.Items.Add(new TreeViewItem() { Content = o.DataField, Tag = o.Id });
    }

    Dispatcher.BeginInvoke(()=>
    {        
        myTreeView.Items.First(o => ((Guid)(o as TreeViewItem).Tag).Equals(idToSelect)).IsSelected = true;
    });
}
  • 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-01T16:26:32+00:00Added an answer on June 1, 2026 at 4:26 pm

    Set property IsSelected inside of Dispatcher.BeginInvoke.

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

Sidebar

Related Questions

I have three xaml files + their code behind files in my Silverlight (C#)
Start a new Silverlight application... and in the code behind (in the Loaded event),
I have a Silverlight application that has a ListBox of a specific width. I
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I have no code behind and the above error keep prompting on INSERT. The
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
NET and VB.net code behind. I have a classic ASP page that connects to
I have an MVVM Silverlight 4 application that holds a list of modules (a
I have a Silverlight 4 application. What would cause the Constructor in my codebehind
I have created a sample silverlight application and trying to populated DataGrid with Data

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.