【问题标题】:Get node's name when click on a graphic node单击图形节点时获取节点的名称
【发布时间】:2020-08-30 13:09:19
【问题描述】:

我现在正在使用 dotviewer 项目。当用户单击节点时,我尝试获取节点的名称。 在此处参考 dotviewer 项目:https://www.codeproject.com/Articles/18870/Dot2WPF-a-WPF-control-for-viewing-Dot-graphs

我检测到当用户点击一个节点时,该节点被下面的代码突出显示

void MouseLeftButtonDownHandler(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    ToolTipController.Hide();

    // Retreive the coordinates of the mouse button event.
    Point pt = e.GetPosition(this);
    DrawingVisual hit = VisualTreeHelper.HitTest(this, pt).VisualHit as DrawingVisual;
    if (hit != null)
    {
        string tag = hit.ReadLocalValue(FrameworkElement.TagProperty) as string;
        if (tag != null)
        {
            foreach (DrawingVisual v in _graph.Children)
            {
                v.BitmapEffect = null;
            }
            OuterGlowBitmapEffect glow = new OuterGlowBitmapEffect();
            glow.GlowColor = Colors.Blue;
            glow.GlowSize = 1;
            glow.Opacity = 0.8;
            glow.Freeze();
            hit.BitmapEffect = glow;
        }
    }
}

代码使用“hit”获取“被舔过的节点”对象,然后高亮显示它

但现在我会得到节点的名称(即图片中的 A 和 B)。有什么办法吗?

【问题讨论】:

  • 你检查过sender是什么吗?
  • sender 是这个当前类(Rodemeyer.Visualizing.GraphElement 类),我找不到类似名称属性的东西

标签: c# wpf graphviz


【解决方案1】:

我找到了解决问题的方法。

string abc = hit.GetValue(FrameworkElement.TagProperty).ToString();
MessageBox.Show(abc);

所以完整的源代码如下所示

void MouseLeftButtonDownHandler(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    ToolTipController.Hide();

    // Retreive the coordinates of the mouse button event.
    Point pt = e.GetPosition(this);
    DrawingVisual hit = VisualTreeHelper.HitTest(this, pt).VisualHit as DrawingVisual;

    string abc = hit.GetValue(FrameworkElement.TagProperty).ToString();
    MessageBox.Show(abc);

    if (hit != null)
    {
        string tag = hit.ReadLocalValue(FrameworkElement.TagProperty) as string;
        if (tag != null)
        {
            foreach (DrawingVisual v in _graph.Children)
            {
                v.BitmapEffect = null;
            }
            OuterGlowBitmapEffect glow = new OuterGlowBitmapEffect();
            glow.GlowColor = Colors.Blue;
            glow.GlowSize = 1;
            glow.Opacity = 0.8;
            glow.Freeze();
            hit.BitmapEffect = glow;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多