Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Binding "SelectedItems" of a Listbox

Binding "SelectedItems" of a Listbox

Scheduled Pinned Locked Moved WPF
helpcsharpdotnetwpfwcf
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jayme65
    wrote on last edited by
    #1

    Hi, Could you please help me figure out how I could bind "SelectedItems" of a listbox? Here I have a 'listbox_2' that contains a list of fruits and vegetables ;) There's another 'listbox_1' that contains groups of fruits and vegetables (under a list format...maybe is it my first error? Should it rather be a list of fruits/vegetables?) and a recipe. I would like that on each group selection (in listbox_1), the corresponding fruits and vegetables would be selected (in listbox_2)...and make it possible to modify that list https://i.imgur.com/nENJTCY.jpg Unfortunately, the way to achieve this behavior stays obscure to me...could you please help? Here's what I've set up untill now: C#

    public partial class MainWindow : Window
    {
    ItemList il;
    GroupList gl;

        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window\_Loaded(object sender, RoutedEventArgs e)
        {
            il = new ItemList();
            ICollectionView cvs = CollectionViewSource.GetDefaultView(il);
            cvs.SortDescriptions.Add(new SortDescription("\_type", ListSortDirection.Ascending));
            cvs.SortDescriptions.Add(new SortDescription("\_name", ListSortDirection.Ascending));
            cvs.GroupDescriptions.Add(new PropertyGroupDescription("\_type"));
            ListBox2.ItemsSource = cvs;
    
            gl = new GroupList();
            ICollectionView cvt = CollectionViewSource.GetDefaultView(gl);
            ListBox1.ItemsSource = cvt;
        } 
    }
    
    public class Item
    {
        public string \_type { get; set; }
        public string \_name { get; set; }
        public Item()
        {
        }
    }
    public class ItemList : ObservableCollection {
        public ItemList() {
            base.Add(new Item() { \_type = "fruit", \_name = "apple" });
            base.Add(new Item() { \_type = "vegetable", \_name = "potato" });
            base.Add(new Item() { \_type = "fruit", \_name = "banana" });
            base.Add(new Item() { \_type = "vegetable", \_name = "tomato" });
            base.Add(new Item() { \_type = "fruit", \_name = "pear" });
            base.Add(new Item() { \_type = "vegetable", \_name = "salad" });
            base.Add(new Item() { \_type = "fruit", \_name = "orange" });
            base.Add(new Item() { \_type = "vegetable", \_name = "onion" }); 
        }
    }
    
    public class Group
    {
        public string \_groupname { get;
    
    L 1 Reply Last reply
    0
    • J Jayme65

      Hi, Could you please help me figure out how I could bind "SelectedItems" of a listbox? Here I have a 'listbox_2' that contains a list of fruits and vegetables ;) There's another 'listbox_1' that contains groups of fruits and vegetables (under a list format...maybe is it my first error? Should it rather be a list of fruits/vegetables?) and a recipe. I would like that on each group selection (in listbox_1), the corresponding fruits and vegetables would be selected (in listbox_2)...and make it possible to modify that list https://i.imgur.com/nENJTCY.jpg Unfortunately, the way to achieve this behavior stays obscure to me...could you please help? Here's what I've set up untill now: C#

      public partial class MainWindow : Window
      {
      ItemList il;
      GroupList gl;

          public MainWindow()
          {
              InitializeComponent();
          }
          private void Window\_Loaded(object sender, RoutedEventArgs e)
          {
              il = new ItemList();
              ICollectionView cvs = CollectionViewSource.GetDefaultView(il);
              cvs.SortDescriptions.Add(new SortDescription("\_type", ListSortDirection.Ascending));
              cvs.SortDescriptions.Add(new SortDescription("\_name", ListSortDirection.Ascending));
              cvs.GroupDescriptions.Add(new PropertyGroupDescription("\_type"));
              ListBox2.ItemsSource = cvs;
      
              gl = new GroupList();
              ICollectionView cvt = CollectionViewSource.GetDefaultView(gl);
              ListBox1.ItemsSource = cvt;
          } 
      }
      
      public class Item
      {
          public string \_type { get; set; }
          public string \_name { get; set; }
          public Item()
          {
          }
      }
      public class ItemList : ObservableCollection {
          public ItemList() {
              base.Add(new Item() { \_type = "fruit", \_name = "apple" });
              base.Add(new Item() { \_type = "vegetable", \_name = "potato" });
              base.Add(new Item() { \_type = "fruit", \_name = "banana" });
              base.Add(new Item() { \_type = "vegetable", \_name = "tomato" });
              base.Add(new Item() { \_type = "fruit", \_name = "pear" });
              base.Add(new Item() { \_type = "vegetable", \_name = "salad" });
              base.Add(new Item() { \_type = "fruit", \_name = "orange" });
              base.Add(new Item() { \_type = "vegetable", \_name = "onion" }); 
          }
      }
      
      public class Group
      {
          public string \_groupname { get;
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      [How to: Use the Master-Detail Pattern with Hierarchical Data | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-use-the-master-detail-pattern-with-hierarchical-data)

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups