Detailed display of object names in hit tests

Asked 2 months ago, Updated 2 months ago, 4 views

https://sites.google.com/site/toriaezunomemo/home/visual-studiomemo/hittotesuto

Using the programs on the site above as a reference, the goal is to implement the hit test program in WPF of C# as follows:

Click
→Display how many figures there are in the clicked part
→ Display Rectangle, Elipse names (Green, Gray, etc.)

But I don't know what to do with the last name acquisition part.

With x.VisualHit.GetType().Name, Elipse and Rectangle can be displayed, but how can I get the corresponding name part?

c# wpf

2022-09-30 11:16

2 Answers

I think the simplest way is to cast to FrameworkElement and look at the Name properties.

 varfe=(FrameworkElement) x.VisualHit;
Console.WriteLine(fe.Name);

A more advanced method is to define your own attachment properties and set the required values.Enter propa on the Visual Studio C# editor and press Tab twice to generate the following code:Please rewrite the highlighted parts.

class Hoge
{
    public static string GetPiyo (DependencyObject obj)
    {
        return(string) obj.GetValue(PiyoProperty);
    }

    public static void SetPiyo (DependencyObject obj, string value)
    {
        obj.SetValue (PiyoProperty, value);
    }

    // Using a DependencyProperty as the backing store for Piyo. This enable animation, styling, binding, etc...
    public static readonly DependencyProperty PiyoProperty=
        DependencyProperty.RegisterAttached("Piyo", typeof(string), typeof(Hoge), new PropertyMetadata(null));
}

After this definition, setting xmlns (local is added by default for Visual Studio 2015) on the XAML allows any string to be set in the property local:Hoge.Piyo, so you can call the Hoge.GetPiyo(fe) at runtime to get the value.


2022-09-30 11:16

If the hit object is FrameworkElement, it has the Name property, so I think I should cast it and get it.

foreach (HitTestResult x in hitResultsList)
{
    US>//#string name=";
    FrameworkElement fe=x.VisualHit as FrameworkElement;
    if(fe!=null)
        name = fe.Name;

    Console.WriteLine("{0}{1}", x.VisualHit.ToString(), name);
}


2022-09-30 11:16

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.