2

我有几个具有以下样式的 ContextMenus:

    <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>

我有一个用于 ListBox 的 ContextMenu,另一个用于按钮(Think Chrome 类型设置菜单)。如果我通过再次单击按钮或单击表单上的任何其他位置关闭按钮的上下文菜单,则关闭列表框,它会很好地关闭,我可以继续正常使用该程序。但是,如果我通过单击 ListBox 关闭 ContextMenu,则只能使用 ListBox,直到单击“n”拖动其中一个 ListBoxItem,之后我可以使用关闭、最小化、搜索等按钮。

窗口图片供参考:

请忽略实际文字:D

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 事件。如果有人能发现问题或需要更多信息,请告诉我。提前致谢。

4

1 回答 1

1

没关系。我解决了这个问题。我所做的是摆脱 UIElement while 语句和相关代码。然后我用一个简单的 TryCast(sender, ListBoxItem) 替换了它。如果您需要更详细的解释,请告诉我,我会尽快回复您。

于 2012-12-19T00:52:31.770 回答