在画布中选择生成的椭圆

时间:2015-10-12 20:23:24

标签: c# wpf canvas ellipse generated

我想获得一个选定的ellpise的名称。

这是我的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方法在画布上获取所选椭圆的名称。但我不知道该怎么做。

有人知道这个伎俩吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您可以访问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
   }
}