Cross-Bound WPF ListBox

I have two lists, one on the left and one on the right. When I select the contactList element in the left Box list, the label information should appear in the right Box list, and this part works fine. The problem I am facing is to do with multi-select, because at the moment it will only display information from a single selection. I changed the select mode in my XAML to multi-select, but that didn't seem to work. I would appreciate any help. Thank.

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>

the 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;

                }

            }
        }
    }
+3
source share
2 answers

I think everyone is confused in this part

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

You are only looking at the first item selected. ( SelectedItems[0]), but considering this one way or another?

, , -

// 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;

tagList, , . ( , , ItemsSource, )

+3

, , , , ListBox , :

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

. DataTemplate, (, , ListBox, ListBoxes), , .

+1

Source: https://habr.com/ru/post/1785990/


All Articles