locked
Multiselect Listbox and current item RRS feed

  • Question

  • This is what I want to do:

    I have two listboxes, let call the first Categories and the other Subcategories. When I scroll the Categories I want to display the subcategories for the current active Category.

    The solution I am looking for is either manually filling the "Subcategories" listbox when current item changes or automatically by using data binding. I can get this working with single select (manually filling just use SelectionChanged event and automatically by using correct databinding).

    However with SelectMode=Multiple this doesn't work with either method since SelectionChanged is only executed when you actually select an item and not just by scrolling the listbox.

    Any ideas?

    I have one possible idea (if there is no other options to be used with Listbox) and that is using a single select listbox but  use embedded checkboxes. Some additional UI issues to fix for that solution though (selecting another item in listbox should give focus to checkbox so space will change isSelected for checkbox).
    Monday, October 6, 2008 4:50 PM

Answers

  • Problem is that I have not found any event which is fired when you change the current item (scrolling the listbox is maybe not the correct word to use since you can scroll the listbox without changing the current item in the listbox).

    When the current item in the listbox is changed (move from one item in listbox to another) I want the subcategory listbox to be updated and displaying the subcategories relation the current category item. This will work in a single select scenario since then SelectionChanged is fired (switching current item IS selecting a new item) and data binding will do it's magic automatically (or you could fill the subcategory listbox manually).

    Doesn't happen when the listbox is set to multi-select. Not what I have been able to figure out at least. Datasource will only change current item when you actually select an item (and it will use the first selected item).

    My solution is to use single select listboxes but have embedded checkboxes (not bound to isSelected of listbox of course). Works fine.
    • Marked as answer by Marco Zhou Friday, October 10, 2008 2:50 AM
    Thursday, October 9, 2008 11:19 AM

All replies

  • Hi,

    See if this helps you

    Xaml:

    <Window x:Class="WpfApplication1.Window1"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="Window1" Height="300" Width="300">  
        <Grid Name="objGrid">  
            <ListBox HorizontalAlignment="Left" Margin="17,50,0,51" Name="listBox1" Width="117" ItemsSource="{Binding}" DisplayMemberPath="Maincategory"  IsSynchronizedWithCurrentItem="True"/>  
            <ListBox HorizontalAlignment="Right" Margin="0,50,13,51" Name="listBox2" Width="117" ItemsSource="{Binding Path=Subcategory}" IsSynchronizedWithCurrentItem="True" />  
        </Grid>  
    </Window>  
      


    Code Behind:
     public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
                List<Category> objCategories = new List<Category>(); 
                for (int i = 0; i < 5; i++) 
                { 
                    Category objCategory = new Category(); 
                    objCategory.Subcategory = new List<string>(); 
                    objCategory.Maincategory = "Category " + i; 
                    for (int j = 0; j <= i; j++) 
                    { 
                        objCategory.Subcategory.Add("SubCategory" + j); 
                    } 
                    objCategories.Add(objCategory); 
                } 
                objGrid.DataContext = objCategories
            } 
        } 


    Category class:
     class Category 
        { 
     
            String maincategory; 
            public String Maincategory 
            { 
                get { return maincategory; } 
                set { maincategory = value; } 
            } 
     
            List<String> subcategory; 
            public List<String> Subcategory 
            { 
                get { return subcategory; } 
                set { subcategory = value; } 
            } 
        } 


    Hope it helps




    FEAR NOT TO BE JUST
    • Proposed as answer by Rahul P Nath Thursday, October 9, 2008 12:24 PM
    Monday, October 6, 2008 7:02 PM
  •  Thank you for taking time to write a detailed reply.

    However the problem is not get the category/subcategory syncronizing working, I have something like your example. Problem is when you have a multi select listbox.

    The "data record" only change (AFAIK) when you select an item in the ListBox but I want to display "Subcategories" also when I move from one item to another in the Categories listbox without selecting the item.

    Also it is actually worse than that with a multiselect listbox. Only the data for the first selected item is displayed (so only subcategories for the first selected category is displayed in subcategories listbox).

    I am hoping for a magic property to set to make the listbox behave the way I want it but I am afraid I am out of luck. I am now experimenting with another approach to the problem:

    I use single select listboxes with "embedded" checkboxes (using an item template). Now the automatic data change will work, but I have a few ui issues to solve (if someone has any ideas how to solve it then please let me know):

    - When user click on any visible checkbox I want the selected item of listbox to switch to the correct item (doesn't happen automatically for some reason).

    - When listbox has the focus I want space to toggle check/uncheck of checkbox (or automatically focus checkbox when item in listbox is selected). Probably easy to do, just haven't done it yet. Not sure which approach is best though. Using a key event checking for space keypress or for example use SelectionChanged to force focus to checkbox.

    I am new to WPF if that is not already obvious :-)

    Monday, October 6, 2008 8:39 PM
  • -> However with SelectMode=Multiple this doesn't work with either method since SelectionChanged is only executed when you actually select an item and not just by scrolling the listbox.

    Well, it's no brainer that scrolling the ListBox will not fire the SelectionChanged event, or I have misunderstood your scenario. To be honest, I have hard time understand your scenario, could you please elaborate which scenario you want to enable using ListBox, and how MultiSelect isn't working in the way you want?

    Thanks
    Thursday, October 9, 2008 6:42 AM
  • Problem is that I have not found any event which is fired when you change the current item (scrolling the listbox is maybe not the correct word to use since you can scroll the listbox without changing the current item in the listbox).

    When the current item in the listbox is changed (move from one item in listbox to another) I want the subcategory listbox to be updated and displaying the subcategories relation the current category item. This will work in a single select scenario since then SelectionChanged is fired (switching current item IS selecting a new item) and data binding will do it's magic automatically (or you could fill the subcategory listbox manually).

    Doesn't happen when the listbox is set to multi-select. Not what I have been able to figure out at least. Datasource will only change current item when you actually select an item (and it will use the first selected item).

    My solution is to use single select listboxes but have embedded checkboxes (not bound to isSelected of listbox of course). Works fine.
    • Marked as answer by Marco Zhou Friday, October 10, 2008 2:50 AM
    Thursday, October 9, 2008 11:19 AM