2016-11-11 4 views
0

子アイテムがエキスパイダーであるListBoxがあります。私はこのためにDragDropイベントを実現する必要があります。私は、XAMLWPF:ListBoxItemとエキスパンダーは、PreviewMouseLeftButtonDownイベントハンドラーを親ListBoxに追加した後に展開できません。

<ListBox PreviewMouseLeftButtonDown="StartDragDrop"> 

を記述する場合、StartDragDrop方法は良い動作しますが、子パンダが拡大することはできませんされています。私は

<ListBox MouseLeftButtonDown="StartDragDrop"> 

を記述する場合、 子パンダが正しい作品ですが、StartDragDrop方法は、作品ではありません。 私は問題がバブルとトンネルイベントと関連していると思うが、私は明確な解決策を知らない。 私は、StartDragDropメソッドとListBox子エキスパンダーの両方が必要です。Expandメソッドが正しいです。私は何をすべきか?

+0

XAMLのビットモードを投稿できますか?これは解決策を考えるには不十分だと私は思う。 –

答えて

0

主なアイデアは、何らかの形での値よりも大きいオフセット移動するときDoDragDrop関数はPreviewMouseMove()イベント、でmathodコールです。

1)ここでは、リストボックス:

<ListBox AllowDrop="True" Drop=" ListBox_Drop" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" PreviewMouseMove="ListBox_PreviewMouseMove"> 

ListBoxItemsたちはDragAndDropを実装した場合に展開することはできませんエキスパンダ、あります。

2)今、私たちは(私はVB.NETを使用)2つの変数を追加する必要があります。

Private Sub ListBox_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) 
    dragStartPoint = e.GetPosition(Me) 
End Sub 

4)PreviewMouseMoveに乗る:

Private isDragging As Boolean = False 'flag: is drag operation in process?' 
Private dragStartPoint As Point 'coords of dragging start.' 

3)は、プレビューマウスクリックでポイントCOORDSを開始覚えておいてください開始点を現在の移動点オフセットに移動する。 offsetがある値より大きい場合、DragAndDrop操作を開始し、フラグisDraggingを設定してこれを思い出します。

Private Sub ListBox_PreviewMouseMove(sender As System.Object, e As MouseEventArgs) 
    If e.LeftButton = MouseButtonState.Pressed Then 
     Dim diff As Vector = Point.Subtract(dragStartPoint, e.GetPosition(Me)) 
        If (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance) OrElse (Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) Then 
      If Not isDragging Then 
             isDragging = True 'the flag is active until drop event raises.' 
                Dim lstBox As ListBox = TryCast(sender, ListBox) 'get sender ListBox' 
                If lstBox IsNot Nothing Then 
                 Dim data As Object = GetDataFromListBox(lstBox, e.GetPosition(lstBox)) 'get data for drag-and-drop; need to be realized; there are some realizations at Stackoverflow.com presented.' 
Dim effects As DragDropEffects = DragDrop.DoDragDrop(lstBox, data, DragDropEffects.Move) 'initiate drag-and-drop.' 

                 End If 
             End If 
         End If 
     End If 
    End Sub 

5)ドロップ操作をProccessing:

Private Sub ListBox_Drop(sender As Object, e As DragEventArgs) 
     isDragging = False 'reset isDragging flag.' 
        Dim lstBox As ListBox = TryCast(sender, ListBox) 'get sender ListBox.' 
        If lstBox IsNot Nothing Then 
         Dim myObj As MyClass = TryCast(e.Data.GetData(GetType(MyClass)), MyClass) 
     '...some actions' 
        End If 
End Sub 

私はこのアイデアを実現してきましたし、まさに私が必要だった作品だ:パンダとMouseLeftButtonClick ListBoxItems上

  • は膨張して 崩壊しています、
  • on MouseMoveドラッグした左ボタンDragAndDrop操作は です。 ListBoxItemsはソートすることができます。
0

トンネリングとバブリングで何かをしなければならないとすれば、あなたは部分的に正しいと思います。外部コントロールのトンネリング(プレビュー付き)イベントは、バブリング(プレビューなし)イベントの前に実行されます。しかし、後者の実行を妨げるものではありません。これはイベントチェーン全体のどこかでe.Handledがtrueに設定されている場合にのみ当てはまります。この例を参照してください:

XAML:あなたが行 "e.Handled =真;" を削除した場合

<Border Background="Red" PreviewMouseMove="OnPreviewMouseMove"> 
    <Border Background="Blue" MouseMove="OnMouseMove" /> 
</Border> 

C#

private void OnPreviewMouseMove(object sender, MouseEventArgs e) 
{ 
    Debug.WriteLine("Preview outer"); 
    e.Handled = true; // this prevents OnMouseMove from being executed 
} 

private void OnMouseMove(object sender, MouseEventArgs e) 
{ 
    Debug.WriteLine("NoPreview inner"); 
} 

は、ONMOUSEMOVEがヒットします。これを自分で設定しない場合は、 "event-name"を基にした呼び出しが可能性があると考えてください。

希望します。

ユルゲン

+0

ありがとうございます。私は月曜日にそれをチェックし、結果について通知します。 – Aave

+0

私は自分のlistviewitemをクリックした後、DoDragDropイベントが発生します。ドラッグアンドドロップでリスト項目を移動することができました。しかし、expander.expandイベントはまだ上昇していません。 – Aave

+0

さて、私はこれを解決しました。主なアイデアは次のとおりです:1)PreviewMouseLeftButtonによる初期点の移動、2)カーソルの移動後のオフセットがMinimumDragDistanceより大きいかどうか、 "isDragging"変数がfalseの場合、3)PreviewMouseMoveイベントでDoDragDropメソッドを呼び出す、4)isDragging = trueを保持するまで落ちる。私はすぐに完全なコードを投稿します。 – Aave

関連する問題