Hi everyone, I tried to summarize and come up with a basic question and my idea, which does not work yet: S
Basically my question is: The user adds the elements together, and I want to create a new element based on these numbers so that I can create a new path for the element defined by the user. Suppose I have a square and a triangle. The user combines this with a home. Now I want to make the house an element for the user. To do this, I need an element path, how do I create this?
My idea The shape elements that are used are based on path lines. Therefore, I want them to be converted to a geometry element, which I can use later. I am using the code provided by André Meneses in the answer below, the code replicated here:
public static Geometry PathMarkupToGeometry(ShieldGearViewModel shieldGearModelRec) { string pathMarkup = shieldGearModelRec.Gear.Path; try { string xaml = "<Path " + "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" + "<Path.Data>" + pathMarkup + "</Path.Data></Path>"; var path = System.Windows.Markup.XamlReader.Load(xaml) as System.Windows.Shapes.Path; // Detach the PathGeometry from the Path if (path != null) { path.Height = shieldGearModelRec.Gear.Height; path.Width = shieldGearModelRec.Gear.Width; path.Fill = new SolidColorBrush(Colors.Green); path.Stretch = Stretch.Fill; Geometry geometry = path.Data; //Test not working, exception is thrown //Rect transRect = new Rect(shieldGearModelRec.Gear.x, shieldGearModelRec.Gear.y, shieldGearModelRec.Gear.Width, shieldGearModelRec.Gear.Height); //geometry.Transform.TransformBounds(transRect); path.Data = null; return geometry; } return null; } catch (Exception ex) { Debug.WriteLine(ex); } return null; }
I did this to get geometry, to follow the example from this link describes . The problem with the above is that I cannot access the x or y elements of the new geometry element, so how can I specify this position?
For the position, I think this link may be a solution, just haven’t got its job yet? :)
When this is done, I will add it to the geometrygroup based on the before link so that I can get the path for the new element. But the geometric group has 0 as borders. Therefore, for this I need to determine x and y for individual elements of the geometry, and then this can solve the problem of the geometric group, or then I should look at it after :) The question was for toooo long: |
TEXT BELOW AN OLD QUESTION AND THOUGHTS
I have a line that I would like to convert to a geometric shape in the code behind. So I found this on Stackoverflow WPF C # Path: how to get from a string with path data to geometry in code (not in XAML)
This link assumes that you can convert a string to a path using parsing, with the following code:
var path = new Path(); path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");
However, parsing is not available on Windows Phone. My other efforts did not solve the problem. I tried using pathGeometry but it doesn't seem to be able to set the string as a path?
So my problem is how to convert a string into a geometric shape into code that is not associated with an element in the view.
The first step , so I managed to create a path with the following
var pathTesting = new System.Windows.Shapes.Path(); var b = new System.Windows.Data.Binding { Source = DecorationOnShield[i].Gear.Path }; System.Windows.Data.BindingOperations.SetBinding(pathTesting, System.Windows.Shapes.Path.DataProperty, b);
Now I'm trying to transform the path to a geometric shape.
Extra
My idea is to do the same thing that this link describes . Where an example is shown:
var blackRectGeometry = new RectangleGeometry(); Rect rct = new Rect(); rct.X = 80; rct.Y = 167; rct.Width = 150; rct.Height = 30; blackRectGeometry.Rect = rct;
But instead of a rectangle, I would like to use an arbitrary shape in the shape of a path, but still be able to set information such as coordinates and size.
Extra
I was thinking of defining a usercontrol that consisted of a path that only looks like this:
<UserControl x:Class="UndoRedoShield.View.Geometry" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8" mc:Ignorable="d" Canvas.Left="{Binding Gear.x}" Canvas.Top="{Binding Gear.y}"> <Path Data="{Binding Gear.Path}" Fill="{Binding Gear.Color}" Stretch="Fill" UseLayoutRounding="False" Height="{Binding Gear.Height}" Width="{Binding Gear.Width}" Opacity="{Binding Gear.Opacity}"> <Path.RenderTransform> <ScaleTransform ScaleX="{Binding Gear.Scale}" ScaleY="{Binding Gear.Scale}"/> </Path.RenderTransform> </Path> </UserControl>
But he could not use it in relation to geometry. Anyone have an idea to use this method? Any method is appreciated! :)
extra extra :)
Is it possible to create a geometric shape from uielements so that the displayed user control can be converted to a geometric path?
Progress
I found this link where I can create geometry from a path. The path has width and height properties.
But the properties that I don't have in the geometry or path are as follows:
- Canvas.Left
- Canvas.Top
- Canvas.ZIndex (I think this is possible when I add it to a GeometryGroup)
It looks like this can be done through the bounds property of Path.Data. But not ZIndex. Therefore, this still needs to be checked with geometryGroup, and the geometry must be added to the GeometryGroup.