开发者

WPF ListBox Multi-Select binding

I have two listBoxes one on the left and one on the right. When I select a 'contactList' item on the left listBox 开发者_StackOverflow社区the 'label' information should be displayed on the right listBox and this part works fine. The problem I am having is to do with multi-select because at the moment it will only display the information from one selection. I changed Selection mode in my XAML to multi-select but that did not seem to work. Would appreciate any assistance. Thanks.

XAML

<Grid x:Name="LayoutRoot" Background="#FFCBD5E6">
    <ListBox x:Name="contactsList" SelectionMode="Multiple" Margin="7,8,0,7" ItemsSource="{Binding ContactLists, Mode=Default}" ItemTemplate="{DynamicResource ContactsTemplate}" HorizontalAlignment="Left" Width="254" SelectionChanged="contactsList_SelectionChanged"/>
    <ListBox x:Name="tagsList" Margin="293,8,8,8" ItemsSource="{Binding AggLabels, Mode=Default}" ItemTemplate="{StaticResource TagsTemplate}" Style="{StaticResource tagsStyle}" />
</Grid>

Code

private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (contactsList.SelectedItems.Count > 0)
        {
            CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
            ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

            ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

            foreach (ContactList contactList in collectionView.Items)
            {
                foreach (AggregatedLabel aggLabel in contactList.AggLabels)
                {
                    labelList.Add(aggLabel);

                    tagsList.ItemsSource = labelList;

                }

            }
        }
    }


I think everyone is confused about this part

CollectionViewGroup collectionView = contactsList.SelectedItems[0] as CollectionViewGroup;
ContactList selectedContact = contactsList.SelectedItems[0] as ContactList;

you're only looking at the first selected item. (SelectedItems[0]), but treating it as one thing or another?

you probably need something like

// only create the list once, outside all the loops
ObservableCollection<AggregatedLabel> labelList = new ObservableCollection<AggregatedLabel>();

foreach (var selected in contactsList.SelectedItems)
{
   // pretty much your existing code here, referencing selected instead of SelectedItems[0]
}

// only set the list once, outside all the loops
tagsList.ItemsSource = labelList;

ideally, you wouldn't be setting the items source on the tagsList, you'd have that bound to a collection already, and you'd just be replacing the contents in this method. (just one call to clear the collection at the top, and no call to set ItemsSource, since it would have already been bound)


I don't really get what you are doing there at all with all that code but how you normally approach the kind of scenario you described is by binding the second ListBox directly to the first one, should look something like this:

<ListBox Name="ListBox1" ItemsSouce="{Binding SomeOriginalSource}" .../>
<ListBox ItemsSouce="{Binding ElementName=ListBox1, Path=SelectedItems}".../>

Edit: You then can either use a DataTemplate which enumerates the internal collections (which for example could cause you to have a ListBox containing other ListBoxes), or you add a converter to the the binding which merges the internal collections into a single collection like John Gardner noted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜