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();
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();
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/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/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);
canvas1.CaptureMouse();
if (rbSelect.IsChecked == true)
{
hitArea = new PointHitTestParameters(startPoint);
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)
{
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;
xpropertyGrid.DataObject = dst;
break;
}
}
break;
}
selectedShape.StrokeThickness = selectedStrokeThickness;
fillBrush = (SolidColorBrush)selectedShape.Fill;
e.Handled = true;
}
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