在WPF列表框中选择多重项仅选择第一个选定项

问题描述 投票:0回答:1

我从下拉列表中选择一个角色后,便有了一个带有“角色”的组合框,列表框下方选择了所选角色具有的权限。但是这里出现了问题,它仅选择了第一个选定的项目。

这一切都像魅力一样,而无需修改设计/模板,但是如果没有它,它看起来不会很好!

代码情况:

<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));
        }

所以我的主要问题是为什么它只选择第一个选定的项目,我该如何解决?

c# wpf xaml selecteditem
1个回答
0
投票

感谢@Andy解决方案。

问题是,ListBox选项IsSynchronizedWithCurrentItem="True"导致ListBox将自身限制为“单一选择模式”,但它并未反映在SelectionMode本身中。

从中更改列表框:

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

为此删除IsSynchronizedWithCurrentItem选项:

<ListBox SelectionMode="Multiple" />

应解决此问题!

© www.soinside.com 2019 - 2024. All rights reserved.