views:

3643

answers:

6

Hi, I have a databound multiselect listbox bound to a datatable. When I select a listboxitem I want some other listboxitems in the same listbox to be selected automatically. I want multiple items to be selected with a single click. How can i do that? I can't do it in SelectionChanged event because it leads to calling the same event again and breaks my logic altogether.

Please help. Any help will be highly appreciated.

A: 

Would having a "SelectedItem" property with the logic in the setter for that property that would handle selecting your other 'like' items?

That's perhaps the way I would go, hard to say with out more details.

Chris Nicol
+5  A: 

Bind IsSelected to a property in your data class. When the property is changed, execute the logic to update the IsSelected property in other data objects:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Then in your data class you can have something like this:

public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        if (_isSelected != value)
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");

            UpdateOtherItems();
        }
    }
}

Or you could have the data item raise an IsSelectedChanged event and have the owning class manage the interdependencies of the selection.

HTH, Kent

Kent Boogaart
A: 

My listbox is already bound to a datatable which has a IsSelected column.I am using the value of this column in a style setter to make the listboxitem selected.Suppose i have 10 rows in the datatable.Now if the user selects the second listboxitem,i can get the isselected of the correspondong row in the database as 1.But how can i get the other items to select at the same time.I think as Kent said,I rather use a property for binding.But how can i use a property to bind a listbox to a datatable....

A: 

I am working on a similar thing.

I have single select combo-boxes that I load with the Selected Value property from the database, and now I am working on multi-select list boxes for-which I have a list of selections in the database I need to bind to the selected list for my list box.

I don't see a way to do it without a loop.

I see listbox read/write properties for getting or setting the Items, SelectedItem/Index/Value, or read only properties for Items or SelectedItems.

A: 

Maybe this is cheating, but, when you are adding the items in the SelectionChanged event have you tried setting IsEnabled false while you selected the multiple items and then setting it back to true afterwords, I think that is supposed to keep the controls events from firing?

A: 

I have created a MultiSelectCollectionView that you might find useful here:

http://grokys.blogspot.com/2010/07/mvvm-and-multiple-selection-part-iii.html

Groky