我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以通过按住 Shift 键并单击来选择列表中的一系列项目。
<ListBox ItemsSource="{Binding ItemFields, Mode=TwoWay}"
VerticalAlignment="Stretch" HorizontalAlignment="Left"
Margin="16,156,0,34" Name="fRListbox" Width="499" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="reportDatagrid_MouseDown"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
在 xaml.cs 上我写了下面的代码:
private void ListItem_MouseClick(object sender, MouseButtonEventArgs e)
{
if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.RightShift))
{
fRListbox.SelectionMode = SelectionMode.Extended;
}
else if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.LeftShift))
{
fRListbox.SelectionMode = SelectionMode.Multiple;
}
else if (e.LeftButton == MouseButtonState.Pressed)
{
fRListbox.SelectionMode = SelectionMode.Multiple;
}
}
但 Shift + 单击功能不起作用。
我是 WPF 新手,谁能指导我。
如果您乐意让用户通过按住来选择项目Ctrl
单击单个项目(例如 Windows 资源管理器和几乎所有其他类型的列表)时按下键,然后设置SelectionMode
to Extended
是使用以下方法实现单选和多选的最简单方法Ctrl
and Shift
keys.
<ListBox ItemsSource="{Binding ValuesView}" SelectionMode="Extended" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)