select 在 canvas 中生成椭圆

select generated ellipse in canvas

我想获取所选椭圆的名称。

这是我的 C# 代码:(后面的代码)

private StackPanel generateSensorObject(int tempvalue, int diameter, string moduletype, string modulename, string macaddress, string lastdate, string description)
    {
        StackPanel s1 = new StackPanel();
        Viewbox v1 = new Viewbox();
        Grid g1 = new Grid();
        ToolTip tool1 = new ToolTip();
        StackPanel st1 = new StackPanel();
        TextBlock tool1t1 = new TextBlock();
        TextBlock tool1t2 = new TextBlock();
        TextBlock tool1t3 = new TextBlock();
        TextBlock tool1t4 = new TextBlock();
        Button b1 = new Button();
        ContextMenu m1 = new ContextMenu();
        MenuItem mi1 = new MenuItem();

        Ellipse e1 = new Ellipse();
        e1.Height = e1.Width = diameter;

        tool1t1.Text = "MAC Addresse: " + macaddress;
        tool1t2.Text = "Typ: " + moduletype;
        tool1t3.Text = "letzter Wert eingetroffen: " + lastdate;
        tool1t4.Text = "Beschreibung: " + description;

        b1.Content = "Diagramm";

        st1.Children.Add(tool1t1);
        st1.Children.Add(tool1t2);
        st1.Children.Add(tool1t3);
        st1.Children.Add(tool1t4);
        st1.Children.Add(b1);
        tool1.Content = st1;

        TextBlock t1 = new TextBlock();
        TextBlock t2 = new TextBlock();
        t1.Text = modulename;
        t2.Text = tempvalue.ToString();
        t2.ToolTip = tool1;
        tool1.StaysOpen = true;

        g1.Children.Add(e1);
        s1.Children.Add(t1);
        g1.Children.Add(t2);
        t2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        t2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
        s1.MouseRightButtonUp += ButtonDiagramm_Click;
        v1.Child = g1;
        s1.Children.Add(v1);

        return s1;
    }

这是我生成椭圆的方法。

现在我正在尝试使用 ButtonDiagram 方法获取 canvas 上选定椭圆的名称。但是我不知道该怎么做。

有人知道这个技巧吗? 谢谢!

你可以访问RoutedEventArgs.Source 属性,如果你的鼠标右键点击某个椭圆,应该传进去:

void ButtonDiagramm_Click(object sender, MouseButtonEventArgs e){
   var ellipse = e.Source as Ellipse;
   if(ellipse != null){
      //do everything with ellipse here, not just access its Name
   }
}