[英]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.感谢@Andy提供的解决方案。
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.问题是 ListBox Option IsSynchronizedWithCurrentItem="True"
导致 ListBox 将自身限制为“单一选择模式”,但并未反映在 SelectionMode 本身中。
Changing the ListBox from:从以下位置更改列表框:
<ListBox IsSynchronizedWithCurrentItem="True" SelectionMode="Multiple" />
do this by removing the IsSynchronizedWithCurrentItem
Option:通过删除IsSynchronizedWithCurrentItem
选项来做到这一点:
<ListBox SelectionMode="Multiple" />
should fix this problem!应该解决这个问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.