次のスタイルのContextMenuがいくつかあります。
<Style TargetType="{x:Type ContextMenu}" x:Key="ListBoxContextMenu">
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Margin="14" Background="White">
<Border.Effect>
<DropShadowEffect Opacity="0.999" BlurRadius="8" ShadowDepth="0"/>
</Border.Effect>
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
リストボックス用に1つのコンテキストメニューがあり、ボタン用にもう1つあります(Chromeタイプの設定メニューを考えてください)。ボタンをもう一度クリックするか、フォーム上の他の場所をクリックしてリストボックスを非表示にしてボタンのコンテキストメニューを閉じると、正常に閉じて、通常どおりプログラムを使用できます。ただし、リストボックスをクリックしてコンテキストメニューを閉じると、リストボックスを使用できるのは「n」をクリックするまでのみです。その後、リストボックスアイテムの1つをドラッグすると、「閉じる」、「最小化」、「検索」などのボタンを使用できます。
参考のためのウィンドウの画像:
ListBoxItemのクリック/ドラッグのコード
Private Sub ReferenceListItemMouseDown(sender As Object, e As MouseButtonEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
StartPoint = e.GetPosition(Nothing)
PW.Resizing = False
End Sub
Private Sub ReferenceListItemMouseMove(sender As Object, e As MouseEventArgs)
Dim PW As MainWindow = Window.GetWindow(MainPage)
If PW.Resizing = False Then
Dim MousePosition As Point = e.GetPosition(Nothing)
Dim Difference As Vector = StartPoint - MousePosition
Dim StopDrop As Boolean
If e.LeftButton = MouseButtonState.Pressed AndAlso (Math.Abs(Difference.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(Difference.Y) > SystemParameters.MinimumVerticalDragDistance) Then
Dim LB As ListBox = ReferenceList
Dim UIE As UIElement = LB.InputHitTest(MousePosition)
If UIE IsNot Nothing Then
Dim Data As Object = DependencyProperty.UnsetValue
While Data Is DependencyProperty.UnsetValue And UIE IsNot Nothing
Data = LB.ItemContainerGenerator.ItemFromContainer(UIE)
If Data Is DependencyProperty.UnsetValue Then
UIE = VisualTreeHelper.GetParent(UIE)
End If
If UIE Is LB Then
StopDrop = True
End If
End While
If Data IsNot DependencyProperty.UnsetValue Then
StopDrop = False
End If
Else
StopDrop = True
End If
PW.TempItem = LB.SelectedItem
Dim FN As String = PW.TempItem.PropLastName & ", " & PW.TempItem.PropFirstName.Substring(0, 1)
Dim TT As String = PW.TempItem.PropTitle
Dim YR As String = PW.TempItem.PropYear.ToString
Dim ReferenceText As String = FN & " " & YR & ", " & TT
Dim DragData As DataObject = New DataObject(DataFormats.StringFormat, ReferenceText)
If DragData IsNot Nothing And StopDrop = False Then
DragDrop.DoDragDrop(sender, DragData, DragDropEffects.Copy)
End If
End If
End If
End Sub
この問題の原因となっているのは、MouseDownイベントとMouseMoveイベントであるという予感があります。誰かが問題を見つけることができるか、さらに情報が必要な場合は、私に知らせてください。前もって感謝します。