none
Best way to clear all selected items in a nested listbox in a itemscontrol RRS feed

  • Question

  • So I want to clear all the selected items in my nested listbox

     <ItemsControl
         <ItemsControl.ItemTemplate>
           <DataTemplate>
                <ItemsControl
                   <ItemsControl.ItemTemplate>
                       <DataTemplate>
                            <ListBox
                               <ListBox.ItemTemplate>
                                   <DataTemplate>

    I guess I could FindChildByType, but that will only give me the first one, and even that seems like it would take a long time.

    Also I'd like to be able to have 1 items selected, press shift and select another items and select all in between.  Now the problem is that they're different listboxes.  I'll probably have to catch the select, see if the shift key is pressed and select all inbetween.

    FYI there could be 1000s of items in the itemscontrol.


    dan

    Friday, October 25, 2013 7:40 PM

Answers

  • I haven't seen this done for an Listbox, but Josh Smith has an article about using checkboxes in a Treeview that might help.  Basically, he has an IsChecked property on each item, as well as a Children property.  You might be able to do something similar with an IsSelected property and then a 'Children' property that is the list of items associated with each of your items shown in the ListBox.  In your IsSelected property, you could have something like the following:

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected= value;
                if ((_isSelected) && (this.Children.Count > 0))
                {
                    foreach (ItemObject child in this.Children)
                    {
                        child.IsSelected = true;
                    }
                }
                base.OnPropertyChanged("IsSelected");
            }
        }
    }

    Then, you can bind IsSelected to the Selected property on the ListBox. 

    Hope this helps!


    Christine A. Piffat

    Friday, October 25, 2013 10:21 PM