我有一個listview對象包含程序中的文件。默認的列表視圖行爲允許我在列表上執行所有選擇操作(單擊+ shift選擇一個塊,單擊+ ctrl單獨選擇一組項目的成員,然後單擊選擇一個項目)。WPF listview drag without deselect
我想通過點擊並按住鼠標左鍵開始拖動這些項目,但它會取消選擇項目...並且即使隨着鼠標移動,它將選擇鼠標移動的任何內容。如何處理鼠標事件以正常進行默認選擇,但如果正在拖動項目,則不會選擇/取消選擇?
如果我處理向下單擊事件,選擇更改同時發生......只有在點擊仍然停止時發生拖動,我知道它是拖放與選擇更改。
下面是控制基本XAML ...
<Window.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseDown" Handler='listView2_MouseLeftButtonDown'/>
</Style>
</Window.Resources>
<ListView Grid.Column="0" Grid.Row="1" Name="listView2" Margin="5,5,5,5" BorderBrush="LightGray" AllowDrop="True" Drop="listView2_Drop" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource itemstyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Bmp}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Ext" DisplayMemberBinding="{Binding Ext}" Width="Auto"/>
<GridViewColumn Header="Size" DisplayMemberBinding="{Binding Size}" Width="Auto"/>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Date}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
好了,我已經與處理previewmousedown和預覽鼠標向上事件......如果控制鍵或Shift按下鍵,我不設置句柄標誌...但除此之外,我將處理的參數設置爲true(所以不會發生選擇更改)。然後在previewmouseup事件中,通過設置「選定」值設置爲真(再次僅在按下shift或ctrl時)。所以這種方法可行......但是shift-block-selection不使用我選擇的任何項目作爲select的有效起點,而是使用shift或ctrl鍵點擊第一個項目(即使我選擇了「已經手動清除了所有選定的項目)。
這裏是源:
private void listView2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (!Keyboard.IsKeyDown(Key.LeftCtrl) &&
!Keyboard.IsKeyDown(Key.RightCtrl) &&
!Keyboard.IsKeyDown(Key.LeftShift) &&
!Keyboard.IsKeyDown(Key.RightShift))
{
e.Handled = true;
}
}
}
private void listView2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
if (!Keyboard.IsKeyDown(Key.LeftCtrl) &&
!Keyboard.IsKeyDown(Key.RightCtrl) &&
!Keyboard.IsKeyDown(Key.LeftShift) &&
!Keyboard.IsKeyDown(Key.RightShift))
{
listView2.SelectedItems.Clear();
ListViewItem lvi = sender as ListViewItem;
listView2.SelectedItem = lvi;
lvi.IsSelected = true;
e.Handled = true;
}
}
}