[WPF][ListBox]鼠标拖拽多选,(Shift Key、Ctrl Key多选有效)



<ListBox Name="listBox" SelectionMode="Extended">

    <ListBox.Resources>

        <Style TargetType="{x:Type ListBoxItem}">

            <EventSetter Event="ListBoxItem.PreviewMouseLeftButtonDown" Handler="lbItem_PreviewMouseLeftButtonDown"/>

            <EventSetter Event="ListBoxItem.PreviewMouseUp" Handler="lbItem_PreviewMouseUp"/>

            <EventSetter Event="ListBoxItem.PreviewMouseMove" Handler="lbItem_PreviewMouseMove"/>

        </Style>

    </ListBox.Resources>

    <x:Type TypeName="DependencyObject"/>

    <x:Type TypeName="Visual"/>

    <x:Type TypeName="UIElement"/>

    <x:Type TypeName="FrameworkElement"/>

    <x:Type TypeName="Control"/>

</ListBox>

private bool inMouseSelectionMode = false;

private List<ListBoxItem> selectedItems = new List<ListBoxItem>(); 

private void lbItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

    // MouseDown时清空已选Item

    // 同时开始"inMouseSelectionMode"

    foreach (var item in selectedItems)

    {

        item.ClearValue(ListBoxItem.BackgroundProperty);

        item.ClearValue(TextElement.ForegroundProperty);

    }

    selectedItems.Clear();

    inMouseSelectionMode = true;

}

private void lbItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)

{

    // MouseUp时停止"inMouseSelectionMode"

    ListBoxItem mouseUpItem = sender as ListBoxItem;

    inMouseSelectionMode = false;



private void lbItem_PreviewMouseMove(object sender, MouseEventArgs e)

{

    ListBoxItem mouseOverItem = sender as ListBoxItem;

    if (mouseOverItem != null && inMouseSelectionMode && e.LeftButton == MouseButtonState.Pressed)

    {

        // Mouse所在的Item设置高亮

        mouseOverItem.Background = SystemColors.HighlightBrush;

        mouseOverItem.SetValue(TextElement.ForegroundProperty, SystemColors.HighlightTextBrush);

        if (!selectedItems.Contains(mouseOverItem)) { selectedItems.Add(mouseOverItem); }

    }

}

  本文来自ny_dsc的博客,原文地址:http://hi.baidu.com/ny_dsc/blog/item/2bc69a31fdfb3186a8018e9a.html

WPF 中,ListBox 默认支持多选。只需要在 ListBox 控件的 SelectionMode 属性中设置为 Multiple,就可以启用多选功能。在多选模式下,用户可以通过 Ctrl 键或 Shift 键选择多个项。 下面是一个简单的示例,演示如何在 ListBox 中启用多选功能: 1. 创建一个新的 WPF 项目,并在 MainWindow.xaml 中添加以下代码: ```XAML <Window x:Class="WpfApp1.MainWindow" ... xmlns:system="clr-namespace:System;assembly=mscorlib"> <StackPanel> <ListBox Name="ListBox" SelectionMode="Multiple"> <system:String>Item 1</system:String> <system:String>Item 2</system:String> <system:String>Item 3</system:String> <system:String>Item 4</system:String> <system:String>Item 5</system:String> </ListBox> <Button Content="Get Selected Items" Click="Button_Click"/> </StackPanel> </Window> ``` 2. 在 MainWindow.xaml.cs 中添加以下代码: ```C# private void Button_Click(object sender, RoutedEventArgs e) { foreach (string item in ListBox.SelectedItems) { MessageBox.Show(item); } } ``` 在上面的代码中,我们创建了一个 ListBox 控件,并将其 SelectionMode 属性设置为 Multiple,以启用多选功能。然后,我们添加了一些 ListBoxItem,用于演示多选功能。 我们还添加了一个按钮,用于在用户选择多个项后获取选定的项。当用户单击按钮时,我们遍历 ListBox.SelectedItems 集合,并使用 MessageBox 显示每个选定的项。 现在,运行程序并选择多个项,然后单击按钮,就可以看到选定的项。 注意:在 WPF 中,ListBoxItem 的默认样式是不带复选框的。如果你想要添加复选框,请参考 WPF 中如何添加复选框到ListBoxItem的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值