2

私はWPFParentUserControlなどを持っていChildUserControlA ChildUserControlBます(WPFコントロールも)

ChildUserControl、他のいくつかの基本的な要素 (テキストボックス、ラベルなど) を含むコントロールです。

メイン フォームのステータス バーに、現在のマウス オーバーの ChildUserControl の名前を表示する必要があります

したがって、ParentUserControlの MouseMove では、 ChildUserControlmyElement = Mouse.DirectlyOverの最上位が必要なため、オブジェクトを取得しますが、代わりに「System.Windows.Controls.TextBlock」(ChildUserControl の一部) を取得します...

それを修正する方法は?

MyChildControl を mouseOver するときに、その子要素ではなく、MyChildControl が検出される必要があります。

この順序で、MyChildControl で次を使用しました。

Protected Overrides Function HitTestCore(
                    hitTestParameters As PointHitTestParameters) As HitTestResult

    Return New PointHitTestResult(Me, hitTestParameters.HitPoint)
End Function

とにかく、結果としてテキストブロックを取得することもあれば、ChildUserControlを取得することもあります...

4

2 に答える 2

3

Hit-Testingを実装する必要があります。したがって、リージョン内にあるすべての要素を取得したい場合は、HitTestResultCallbackを使用できます。例:

Dim Elements As New List(Of FrameworkElement)


Public Function GetVisuals(ByVal Region As Geometry) As List(Of FrameworkElement)

    If Region Is Nothing Then Return Nothing

    Dim Parameters As New GeometryHitTestParameters(Region)
    Elements.Clear()

    Dim Callback As New HitTestResultCallback(AddressOf Me.HitTestCallBack)
    VisualTreeHelper.HitTest(Me.ParentUserControl, Nothing, Callback, Parameters)

    Return Me.Elements

End Function

Private Function HitTestCallBack(ByVal Result As HitTestResult) As HitTestResultBehavior

    If Result IsNot Nothing Then

        Dim GeometryRes As GeometryHitTestResult = CType(Result, GeometryHitTestResult)
        Dim Element As FrameworkElement = TryCast(Result.VisualHit, FrameworkElement)

        If Element IsNot Nothing AndAlso GeometryRes.IntersectionDetail = IntersectionDetail.FullyContains Then
            Me.Elements.Add(Element)
        End If

    End If

    Return HitTestResultBehavior.Continue

End Function

このように、Elements リストにはGeometryHitTestResult.IntersectionDetailで指定されたすべての要素が含まれます。(たとえば) マウス位置 (MouseDown イベント) の下の要素を知りたい場合は、単純に次のようにします。

Dim Region As New RectangleGeometry(New Rect(e.GetPosition(Me.MyGrid), New Size(1, 1)))
Dim Elements As List(Of FrameworkElement) = Me.GetVisuals(Region)

これが私のコメントの意味です:

Public Class DrawingCanvas
Inherits Panel

Public Function GetVisuals(ByVal Region As Geometry) As List(Of DrawingVisual)
End Function

Private Function HitTestCallBack(ByVal Result As HitTestResult) As HitTestResultBehavior
End Function

End Class
于 2012-04-17T16:43:06.887 に答える
1

別の答えは次のようなものかもしれません - 気になるオブジェクトの正確なタイプを定義し、それらが見つかるまでビジュアルツリーを調べてください:

Private myTypes As New List(Of Type)()
Public Sub New()
    InitializeComponent()
    myTypes.Add(GetType(ComboBox))
    myTypes.Add(GetType(CheckBox))
    myTypes.Add(GetType(RadioButton))
    myTypes.Add(GetType(TabControl))
    myTypes.Add(GetType(Button))
    myTypes.Add(GetType(Label))
    myTypes.Add(GetType(GroupBox))
    myTypes.Add(GetType(Window))
End Sub

Private Sub Window_MouseMove(sender As Object, e As MouseEventArgs)
    Dim x As DependencyObject = _
      DirectCast(e.MouseDevice.DirectlyOver, DependencyObject)
    Dim t As Type = x.GetType
    While Not myTypes.Contains(t)
         x = VisualTreeHelper.GetParent(x)
         If x Is Nothing Then Exit While
         t = x.GetType
    End While

    If x IsNot Nothing Then
          Console.WriteLine(x.ToString())
    Else
          Console.WriteLine("Nothing")
    End If
End Sub
于 2012-04-17T16:53:44.650 に答える