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

  • Home
  • SEARCH
  • 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 8039003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:25:46+00:00 2026-06-05T03:25:46+00:00

I am using MVVM light and have a list box with multiple selection. In

  • 0

I am using MVVM light and have a list box with multiple selection. In my Mainpage.xaml I have

<ListBox Name="ListBox1" ItemsSource="{Binding Items}" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"  Margin="15,15,18,0" SelectionMode="Multiple" Height="100" />

In MainPage.xaml.cs I have (I do not want to use dependency property for some reason).

MainPage()
{
    ListBox1.SelectionChanged = new SelectionChangedEventHandler(ListBox1_SelectionChanged);
}

void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 var listBox = sender as ListBox;
 var viewModel = listBox.DataContext as MainViewModel;
 viewModel.SelectedItems.Clear();
 foreach (string item in listBox.SelectedItems)
     viewModel.SelectedItems.Add(item);
}

and this works fine and binds to my MainViewModel. But When the page is loaded I want the first item of the the collection items to be selected by default. Please let me know how to implement this

  • 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-05T03:25:48+00:00Added an answer on June 5, 2026 at 3:25 am

    I’d recommend using the ListBox’s Loaded event and then bind to the first item in the collection:

    MainPage()
    {
        ListBox1.Loaded += new RoutedEventHandler( OnListBox1Loaded );
        ListBox1.SelectionChanged += new SelectionChangedEventHandler(ListBox1_SelectionChanged);
    }
    
    private void OnListBox1Loaded( object sender, RoutedEventArgs e )
    {
        // make sure the selection changed event doesn't fire
        // when the selection changes
        ListBox1.SelectionChanged -= MyList_SelectionChanged;
    
        ListBox1.SelectedIndex = 0;
        e.Handled = true;
    
        // re-hook up the selection changed event.
        ListBox1.SelectionChanged += MyList_SelectionChanged;
    }
    

    Edit

    If you can not use the Loaded event, then you’ll need to create another property in your Model that will hold the item you want selected and then assign that property to the SelectedItem property of the ListBox.

    public class MyModel : INotifyPropertyChanged
    {
    
      private ObservableCollection<SomeObject> _items;
      public ObservableCollection<SomeObject> Items
      {
        get { return _items; }
        set
        {
            _items = value;
            NotifyPropertyChanged( "Items" );
        }
      }
    
      private SomeObject _selected;
      public SomeObject  Selected
      {
        get { return _selected; }
        set
        {
            _selected = value;
            NotifyPropertyChanged( "Selected" );
        }
      }
    
      public void SomeMethodThatPopulatesItems()
      {
        // create/populate the Items collection
    
        Selected = Items[0];
      }
    
      // Implementation of INotifyPropertyChanged excluded for brevity
    
    }
    

    XAML

    <ListBox ItemsSource="{Binding Path=Items}" 
             SelectedItem="{Binding Path=Selected}"/>
    

    By having another property that holds the currently selected item, you also have access in your Model to that item as well whenever the selected item is changed by the user.

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

Sidebar

Related Questions

I am using Mvvm-Light and have been ignoring how binding in XAML really works
I'm using MVVM (MVVM Light Toolkit) and have a property on the view model
Using MVVM Light, I have two WPF applications that reference a common Views library.
I'm using the MVVM Light Toolkit and I have a DataGrid bound to an
I am new to MVVM . I have list box that populates the list
I'm new to Prism, but I have successfully built several WPF/Mvvm-Light applications. I'm using
Hi I have a silverlight MVVM application using MVVM light. When I open the
I'm using the MVVM Light Toolkit with Silverlight. On my UserControl I have a
I am using MVVM Light. I have created a window that looks like this:
I have created a Treeview in WPF MVVM (Project is using MVVM Light). When

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.