简体   繁体   中英

Selecting mutliple items in a WPF ListBox only selects the first selected item

I have a ComboBox with "roles" in it after selecting one role from the dropdown, below a ListBox selects the permissions the selected role has. But here comes the problem it only selects the first selected item.

It all works like a charm without modifying the design / template but it doesn't look good without it!

Code Situation:

<ComboBox Name="AccessRoleBx" MinWidth="190" Height="35" SelectionChanged="AccessRoleBx_OnSelected" HorizontalContentAlignment="Center"
                                      VerticalContentAlignment="Center" />

<ListBox MaxHeight="225" Height="Auto" Width="335" Name="AccessPermissionBx" SelectionMode="Multiple" IsSynchronizedWithCurrentItem="True">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <CheckBox Margin="5,2"
                                                      IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem, AncestorLevel=1}, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"
                                                      Unchecked="ToggleButton_OnUnchecked"
                                                      Checked="ToggleButton_OnChecked">
                                                <TextBlock Text="{Binding}" />
                                            </CheckBox>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
        private void AccessRoleBx_OnSelected(object sender, RoutedEventArgs e)
        {
            if (AccessRoleBx.SelectedItem is AccessRole accessRole)
            {
                accessRole.Load(_app);
                AccessPermissionBx.UnselectAll();
                foreach (var ap in accessRole.Permissions)
                {
                    AccessPermissionBx.SelectedItems.Add(ap);
                }

                AccessPermissionBx.Items.Refresh();
                SelectedRoleBx.Text = $"({accessRole.Name})";
            }
        }

        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                if (cb.Content is ContentPresenter cp)
                {
                    if (cp.Content is ActionPermission ap)
                    {
                        if (!AccessPermissionBx.SelectedItems.Contains(ap))
                        {
                            AccessPermissionBx.SelectedItems.Add(ap);
                        }

                        AccessPermissionBx.Items.Refresh();
                    }
                }
            }
        }

        private void ToggleButton_OnUnchecked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                if (cb.Content is ContentPresenter cp)
                {
                    if (cp.Content is ActionPermission ap)
                    {
                        if (AccessPermissionBx.SelectedItems.Contains(ap))
                        {
                            AccessPermissionBx.SelectedItems.Remove(ap);
                        }

                        AccessPermissionBx.Items.Refresh();
                    }
                }
            }
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            var bw = new BackgroundWorker();

            bw.DoWork += (o, args) =>
            {
                Dispatcher.Invoke(() =>
                {
                    AccessRoleBx.ItemsSource = _requests.GetAccessRoles();
                    AccessPermissionBx.ItemsSource = Enum.GetValues(typeof(ActionPermission));

                    AccessRoleBx.SelectedIndex = 0;

                    AccessPermissionBx.Items.Refresh();
                });
            };
            //bw.RunWorkerCompleted += (o, args) => { };
            bw.RunWorkerAsync();

            AccessRoleCreateBtn.IsEnabled = false;

            AccessRoleBx.DisplayMemberPath = "Name";
            AccessRoleBx.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        }

So my main question is why does it only select the first selected item and how can i fix that?

Thanks to @Andy for the solution.

The problem was that the ListBox Option IsSynchronizedWithCurrentItem="True" caused the ListBox to limit itself to a "Single Selection Mode" doesn't reflect in the SelectionMode itself though.

Changing the ListBox from:

<ListBox IsSynchronizedWithCurrentItem="True" SelectionMode="Multiple" />

do this by removing the IsSynchronizedWithCurrentItem Option:

<ListBox SelectionMode="Multiple" />

should fix this problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM