views:

310

answers:

1

I have two ListBoxes, both use Extended SelectionMode. The ItemsSource of the first is a List, and uses a datatemplate. I'm trying to use an aggregation of some property from the first as the itemssource for the second. For example:

public class MultiAppPropertyAggregator : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    IList<SomeObject> selectedItems = value as IList<SomeObject>;
    Dictionary<string, string> bundles = new Dictionary<string,string>();
    foreach(SomeObject myobj in selectedItems) {
        foreach(KeyValuePair<string,string> name in myobj.Names) {
           selectedItems.Add(name.Key, name.Value);
....

<ListBox x:Name="lstApplication" ItemsSource="{Binding}" SelectionChanged="lstApplication_SelectionChanged" SelectionMode="Extended" />
<ListBox x:Name="lstBundles" ItemsSource="{Binding ElementName=lstApplication,Path=SelectedItems,Mode=OneWay,Converter={StaticResource MultiAppPropertyAggregator}}" ItemTemplate="{StaticResource DictionaryList}" SelectedValuePath="Key" SelectionMode="Extended" />

So the objects in the first list contain a property of type Dictionary. I want to add all items in the dictionaries of all selected items in the first list to the second list.

The converter seems to be called on initial load, then not again after that and I end up with an empty second listbox. Am I missing something?

A: 

I'd guess that you're converter is only being called once because SelectedItems on a list box is not a DependencyProperty and, therefore, will not notify the binding that it has updated.

You may be better off doing this conversion in your codebehind/viewmodel (depending on which methodology you follow) and exposing a property for the second list box to bind to.

You can do this in one of two ways that I can think of. First, you can listen to SelectionChanged on the first list and update the property that the second list is bound to. Or, you can put an IsSelected property on the items that the first list is bound to and update your second list when that changes on any given item. You can add this style for ListBoxItem to sync the IsSelected property between the data item and the view:

<Style TargetType="{x:Type ListBoxItem}">
   <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
</Style>

My guess is that the first one will be less difficult to implement, though it may not fully mesh with whatever UI methodology you're following.

dustyburwell
Thanks, the first method of listening for the SelectionChanged event and setting the ItemsSource every time works perfectly.
Echilon