Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok..
i creating a wpf application in which if i dragged shapes to the canvas, they are added to the canvas.


But i have a selectshape button which i am using to select shape after which if i select a particular shape it should display the properties of that shape on the property editor.

this is my code for the xaml...

<Border BorderThickness="2" BorderBrush="LightBlue" Margin="106,41,208,0" Width="390">
          <Canvas Name="canvas1" Background="Transparent" MouseLeftButtonDown="OnMouseLeftButtonDown" MouseMove="OnMouseMove" MouseLeftButtonUp="OnMouseLeftButtonUp" Height="366" Width="386">

          </Canvas>
      </Border>


this the method that addshapes...this works well..

private void AddShape(Point pt1, Point pt2,  string s) 
        {
            path = new Path();
            path.Fill = fillBrush;
            path.Width = canvas1.Width;
            
            path.Height = canvas1.Height;
            path.Stroke = strokeBrush; 
            
            path.StrokeThickness = originalStrokeThickness; 
            if (s == "rectangle") 
            { 
                RectangleGeometry geometry = new RectangleGeometry(); 
                //double width = Math.Abs(pt1.X - pt2.X); 
                //double height = Math.Abs(pt1.Y - pt2.Y); 
                shapename = "Rectangle";
                normalizepoints();
                left = Math.Min(startPoint.X, currentPoint.X); 
                top = Math.Min(startPoint.Y, currentPoint.Y); 
                geometry.Rect = new Rect(left, top, width, height); 
                path.Data = geometry;
                path.Name=shapename;
                savedShape.Add(path);
            }
            else if (s == "ellipse")
            {
                EllipseGeometry geometry = new EllipseGeometry();
                //double width = Math.Abs(pt1.X - pt2.X);
                //double height = Math.Abs(pt1.Y - pt2.Y);
                shapename = "Ellipse";
                normalizepoints();
                 left = Math.Min(startPoint.X, currentPoint.X);
                 top = Math.Min(startPoint.Y, currentPoint.Y);
               
                geometry.Center = new Point(left + width / 2, top + height / 2);
                geometry.RadiusX = width / 2;
                geometry.RadiusY = height / 2;
               
                path.Data = geometry;
                path.Name = shapename;
                savedShape.Add(path);
            }
            else if (s == "hexagon")
            {
                shapename = "Hexagon";
                normalizepoints();
                if (startPoint.X < currentPoint.X)
                {
                    PolyLineSegment hexa = new PolyLineSegment(new Point[] 
            {
                
                //new Point(startPoint.X, startPoint.Y),
                //new Point(startPoint.X+width/2, startPoint.Y+height),
                //new Point(startPoint.X+width, startPoint.Y),
                
                
                new Point(startPoint.X, startPoint.Y),
                new Point(startPoint.X+width/4, startPoint.Y-height/2),
                new Point(startPoint.X+(0.75*width), startPoint.Y-height/2),
                new Point(startPoint.X+width, startPoint.Y),
                new Point(startPoint.X+(0.75*width), startPoint.Y+height/2),
                new Point(startPoint.X+width/4, startPoint.Y+height/2),
                new Point(startPoint.X, startPoint.Y)
                               

            }, true);

                    PathFigure pa = new PathFigure(new Point(startPoint.X, startPoint.Y), new PathSegment[] { hexa }, true
                       );
                    PathGeometry pg = new PathGeometry(new PathFigure[] { pa });
                    path.Name = shapename;
                    path.Data = pg;
                    savedShape.Add(path);
                
                }
                else
                {
                    PolyLineSegment hexd = new PolyLineSegment(new Point[] 
            {
                
                //new Point(startPoint.X, startPoint.Y),
                //new Point(startPoint.X+width/2, startPoint.Y+height),
                //new Point(startPoint.X+width, startPoint.Y),
                
                
                new Point(startPoint.X, startPoint.Y),
                new Point(startPoint.X-width/4, startPoint.Y-height/2),
                new Point(startPoint.X-(0.75*width), startPoint.Y-height/2),
                new Point(startPoint.X-width, startPoint.Y),
                new Point(startPoint.X-(0.75*width), startPoint.Y+height/2),
                new Point(startPoint.X-width/4, startPoint.Y+height/2),
                new Point(startPoint.X, startPoint.Y)
                   

                }, true);

                    PathFigure pa = new PathFigure(new Point(startPoint.X, startPoint.Y), new PathSegment[] { hexd }, true
                   );
                    PathGeometry pg = new PathGeometry(new PathFigure[] { pa });
                    path.Name = shapename;
                    path.Data = pg;
                    savedShape.Add(path);
                
                }
               
               
                
            }
            
            canvas1.Children.Add(path);
            showproperty();
            Canvas.SetTop(path, 0);
            Canvas.SetLeft(path, 0);
            
        }
.



But if i select a shape on the canvas it doesnot update the property editor,,,although i created a propertyclass with properties of width, height, and name..

this is my code for the selection...

if (!canvas1.IsMouseCaptured)
           {
              startPoint = e.GetPosition(canvas1);
           //   selectedPoint = e.GetPosition(canvas1);

               canvas1.CaptureMouse();
               if (rbSelect.IsChecked == true)
               {
                   hitArea = new PointHitTestParameters(startPoint);


                   // Call HitTest method:
                   VisualTreeHelper.HitTest(canvas1, null,
                       new HitTestResultCallback(HitTestCallback),
                       new PointHitTestParameters(startPoint));




               Breakloops:
                   selectedShape = (Path)e.Source;
               foreach (PropertyClass datareal in classdata)
               {

                   foreach (Path path in savedShape)
                   {
                      // MessageBox.Show(path.GeometryTransform.DependencyObjectType.Name);
                       path.StrokeThickness = originalStrokeThickness;
                       if ((path.Name == "Rectangle") && (datareal.Name == "Rectangle"))
                       {
                           var dst = new PropertyClass();
                           dst.Height = datareal.Height;
                           dst.Width = datareal.Width;
                           dst.Name = datareal.Name;
                           xpropertyGrid.DataObject = dst;
                           break;

                       }
                       if ((path.Name == "Ellipse") && (datareal.Name == "Ellipse"))
                       {
                           var dst = new PropertyClass();
                           dst.Height = datareal.Height;
                           dst.Width = datareal.Width;
                           dst.Name = datareal.Name;
                           MessageBox.Show(dst.Name.ToString());
                           xpropertyGrid.DataObject = dst;
                           break;

                       }

                       if ((path.Name == "Hexagon") && (datareal.Name == "Hexagon"))
                       {
                           var dst = new PropertyClass();
                           dst.Height = datareal.Height;
                           dst.Width = datareal.Width;
                           dst.Name = datareal.Name;
                           // MessageBox.Show(dst.Name.ToString());
                           xpropertyGrid.DataObject = dst;
                           break;

                       }

                       // MessageBox.Show(path.Name);

                   }
                   break;
               }

                   selectedShape.StrokeThickness = selectedStrokeThickness;
                   fillBrush = (SolidColorBrush)selectedShape.Fill;
                   e.Handled = true;
                   //pst.Height=






               }


What I have tried:

I have tried using the hittest, but the point is my selectedshape does not tell me what shape i am selecting..

i also collected the shapes to a list, and using a foreach statement to go throught, but all I want is that while looping through, just get me the selected shape so that i can update its properties in the property window
Posted
Comments
Erik Rude 11-Jul-17 8:02am    
Have you implemented INotifyPropertyChanged for your objects?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900