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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:31:47+00:00 2026-05-12T09:31:47+00:00

I have a multi-select listbox in a SL3 app using prism and I need

  • 0

I have a multi-select listbox in a SL3 app using prism and I need a collection in my viewmodel that contains the currently selected items in the listbox.

The viewmodel doesn’t know anything about the view so it does not have access to the listbox control. Also I need to be able to clear the selected items in the listbox from the viewmodel.

Not sure how to approach this problem

thanks
Michael

  • 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-12T09:31:47+00:00Added an answer on May 12, 2026 at 9:31 am

    So, assume you have a ViewModel with the following properties:

    public ObservableCollection<string> AllItems { get; private set; }
    public ObservableCollection<string> SelectedItems { get; private set; }
    

    You would start by binding your AllItems collection to the ListBox:

    <ListBox x:Name="MyListBox" ItemsSource="{Binding AllItems}" SelectionMode="Multiple" />
    

    The problem is that the SelectedItems property on ListBox is not a DependencyProperty. This is pretty bad, since you can’t bind it to something in your ViewModel.

    The first approach is to just put this logic in the code-behind, to tweak the ViewModel:

    public MainPage()
    {
        InitializeComponent();
    
        MyListBox.SelectionChanged += ListBoxSelectionChanged;
    }
    
    private static void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBox = sender as ListBox;
        if(listBox == null) return;
    
        var viewModel = listBox.DataContext as MainVM;
        if(viewModel == null) return;
    
        viewModel.SelectedItems.Clear();
    
        foreach (string item in listBox.SelectedItems)
        {
            viewModel.SelectedItems.Add(item);
        }
    }
    

    This approach will work, but it is really ugly. My preferred approach is to extract this behavior into an “Attached Behavior”. If you do that, you can completely eliminate your code-behind and set it up in the XAML. The bonus is that this “Attached Behavior” is now re-usable in any ListBox:

    <ListBox ItemsSource="{Binding AllItems}" Demo:SelectedItems.Items="{Binding SelectedItems}" SelectionMode="Multiple" />
    

    And here is the code for the Attached Behavior:

    public static class SelectedItems
    {
        private static readonly DependencyProperty SelectedItemsBehaviorProperty =
            DependencyProperty.RegisterAttached(
                "SelectedItemsBehavior",
                typeof(SelectedItemsBehavior),
                typeof(ListBox),
                null);
    
        public static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterAttached(
                "Items",
                typeof(IList),
                typeof(SelectedItems),
                new PropertyMetadata(null, ItemsPropertyChanged));
    
        public static void SetItems(ListBox listBox, IList list) { listBox.SetValue(ItemsProperty, list); }
        public static IList GetItems(ListBox listBox) { return listBox.GetValue(ItemsProperty) as IList; }
    
        private static void ItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as ListBox;
            if (target != null)
            {
                GetOrCreateBehavior(target, e.NewValue as IList);
            }
        }
    
        private static SelectedItemsBehavior GetOrCreateBehavior(ListBox target, IList list)
        {
            var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior;
            if (behavior == null)
            {
                behavior = new SelectedItemsBehavior(target, list);
                target.SetValue(SelectedItemsBehaviorProperty, behavior);
            }
    
            return behavior;
        }
    }
    
    public class SelectedItemsBehavior
    {
        private readonly ListBox _listBox;
        private readonly IList _boundList;
    
        public SelectedItemsBehavior(ListBox listBox, IList boundList)
        {
            _boundList = boundList;
            _listBox = listBox;
            _listBox.SelectionChanged += OnSelectionChanged;
        }
    
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _boundList.Clear();
    
            foreach (var item in _listBox.SelectedItems)
            {
                _boundList.Add(item);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The reson RegEx.Split is returning four values is that you… May 13, 2026 at 1:21 am
  • Editorial Team
    Editorial Team added an answer The problem you'll face is that there's no real inherent… May 13, 2026 at 1:21 am
  • Editorial Team
    Editorial Team added an answer Use a QFutureWatcher to monitor the QFuture object using signals… May 13, 2026 at 1:21 am

Related Questions

I have a multi-select listbox which I am binding to a DataTable. DataTable contains
I have a form that contains a GridView control which is databound to an
I'm trying to build an edit page for a database record that can be
I have a multi-select <asp:listbox> and a button. I want to disable the button

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.