[WPF] Drag d'un ListView Item > la sélection sur ce ListView est alors plus chaotique
Bonjour,
Dans le code suivant, l'utilisateur peut glisser un 'ListView Item' vers le navigateur WEB, la page correspondante s'ouvre alors.
Cela marche..mais je ne suis pas sur que je m'y sois pris de la meilleure façon car depuis que le drag est rendu possible depuis ce ListView la sélection d'un élément est plus chaotique (c-à-d qu'il faut très souvent s'y reprendre à plusieurs fois pour SELECTIONNER un élément).
Pourriez-vous m'aider svp? Merci !!
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <Window x:Class="MainWindow"
xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Name="PersonListView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="ListViewItem.PreviewMouseLeftButtonDown" Handler="PersonListView_MouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn DisplayMemberBinding="{Binding Url}" Width="300">
<GridViewColumnHeader>Url</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Width="100">
<GridViewColumnHeader>Name</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim personList As New List(Of Person)()
personList.Add(New Person() With {.Url = "http://www.apple.com", .Name = "Apple"})
personList.Add(New Person() With {.Url = "http://www.hp.com", .Name = "HP"})
personList.Add(New Person() With {.Url = "http://www.asus.com", .Name = "Asus"})
PersonListView.ItemsSource = personList
End Sub
Private Sub PersonListView_MouseLeftButtonDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs)
If TypeOf sender Is ListViewItem Then
Dim o1 As Person = TryCast(sender.DataContext, Person)
If o1 IsNot Nothing Then
Dim url As String = o1.Url
Dim dataObject As New DataObject(DataFormats.Text, url)
DragDrop.DoDragDrop(Me, dataObject, DragDropEffects.Copy)
End If
End If
End Sub
End Class
Public Class Person
Public Property Url() As String
Get
Return m_Id
End Get
Set(value As String)
m_Id = value
End Set
End Property
Private m_Id As String
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
End Class |