【发布时间】:2014-03-12 14:23:15
【问题描述】:
我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以通过 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 新手,谁能指导我。
【问题讨论】: