Juan Pablo Gomez
Juan Pablo Gomez

Reputation: 5534

Create Path programmatically doesn't works as Desired

I Have this custom control

        <Grid Name="PanelInferior" Grid.Row="5" Grid.ColumnSpan="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="NombrePiezaHolder" Width="2*"/>
                <ColumnDefinition x:Name="Simbol1" Width="1*"/>
                <ColumnDefinition x:Name="Simbol2" Width="1*"/>
                <ColumnDefinition x:Name="Simbol3" Width="1*"/>
                <ColumnDefinition x:Name="Simbol4" Width="1*"/>                
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <TextBlock x:Name="NombrePieza" Text="{Binding ElementName=Esta_Pieza,Path=Pieza.Id}" Margin="0" Height="Auto" Width="Auto" Panel.ZIndex="0" VerticalAlignment="Center" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" FontWeight="ExtraBold" HorizontalAlignment="Center"/>
            <Viewbox x:Name="PanelInferior1" Grid.Row="0" Grid.Column="1" Height="Auto" Width="Auto">
                <Grid x:Name="PanelInferior1Contenedor" >
                </Grid>
            </Viewbox>
            <Viewbox x:Name="PanelInferior2" Grid.Row="0" Grid.Column="2" Height="Auto" Width="Auto">
                <Grid x:Name="PanelInferior2Contenedor">
                    <Path x:Name="EndodonciaPorRealizar" Data="M-2.1394273E-06,993.43869 L68.000003,995 32.942219,2.8478794E-06 z" Height="200" Margin="0" Stretch="Fill" Width="200" Fill="#FFBB2727"/>
                </Grid>
            </Viewbox>
            <Viewbox x:Name="PanelInferior3" Grid.Row="0" Grid.Column="3" Height="Auto" Width="Auto">
                <Grid x:Name="PanelInferior3Contenedor">
                </Grid>
            </Viewbox>
            <Viewbox x:Name="PanelInferior4" Grid.Row="0" Grid.Column="4" Height="Auto" Width="Auto">
                <Grid x:Name="PanelInferior4Contenedor">
                </Grid>
            </Viewbox>
        </Grid>
    </Grid>
</UserControl>

The Grid PanelInferior has 4 places to draw in it at run time, in this simple the Column 2 has allready a path in it(just for tests) if I add another Path with the same properties it doesn't draw like the other one (Column2)

This is the string Passed to de TextToPath function:

x:Name="EndodonciaPorRealizar"~Data="M-2.1394273E-06,993.43869 L68.000003,995 32.942219,2.8478794E-06"~Height="200"~Margin="0"~Stretch="Fill"~Width="200"~Fill="#FFBB2727"

This is the code that creates the path at run time

   public static Path TextToPath(String PathText, string name )
    {
        Path NewPath = new Path();
        NewPath.Name = name;
        string PropertieName = "";
        string PropertieValue = "";
        string[] Properties = PathText.Split('~');
        foreach (string Propertie in Properties)
        {
            string[] PropertieParts = Propertie.Split('=');
            PropertieName = PropertieParts[0];
            PropertieValue = PropertieParts[1].Replace('"', " ".ToCharArray()[0]);
            switch (PropertieName)
            {
                case "Data":
                    NewPath.Data = Geometry.Parse(PropertieValue);
                    break;
                case "Height":
                    if (PropertieValue == " Auto ")
                        NewPath.Height = Double.NaN;
                    else
                        NewPath.Height = double.Parse(PropertieValue);
                    break;
                case "Width":
                    if (PropertieValue == " Auto ")
                        NewPath.Width = Double.NaN;
                    else
                        NewPath.Width = double.Parse(PropertieValue);
                    break;
                case "Fill":
                    NewPath.Fill = (SolidColorBrush)(new BrushConverter().ConvertFrom(PropertieValue));
                    break;
                case "Margin":
                    string[] Margins = PropertieValue.Split(',');
                    switch (Margins.Count())
                    {
                        case 1:
                            NewPath.Margin = new Thickness(double.Parse(PropertieValue));
                            break;
                        case 4:
                            NewPath.Margin = new Thickness(
                                double.Parse(Margins[0]),
                                double.Parse(Margins[1]),
                                double.Parse(Margins[2]),
                                double.Parse(Margins[3])
                                );
                            break;
                    }
                    break;
                case "HorizontalAlignment":
                    switch (PropertieValue)
                    {
                        case "Left":
                            NewPath.HorizontalAlignment = HorizontalAlignment.Left;
                            break;
                        case "Center":
                            NewPath.HorizontalAlignment = HorizontalAlignment.Center;
                            break;
                        case "Right":
                            NewPath.HorizontalAlignment = HorizontalAlignment.Right;
                            break;
                        case "Stretch":
                            NewPath.HorizontalAlignment = HorizontalAlignment.Stretch;
                            break;
                    }
                    break;
                case "Stretch":
                    switch (PropertieValue)
                    {
                        case "Fill":
                            NewPath.Stretch = Stretch.Fill;
                            break;
                        case "None":
                            NewPath.Stretch = Stretch.None;
                            break;
                        case "Uniform":
                            NewPath.Stretch = Stretch.Uniform;
                            break;
                        case "UniformToFill":
                            NewPath.Stretch = Stretch.UniformToFill;
                            break;
                    }
                    break;
                case "VerticalAlignment":
                    switch (PropertieValue)
                    {
                        case "Bottom":
                            NewPath.VerticalAlignment = VerticalAlignment.Bottom;
                            break;
                        case "Center":
                            NewPath.VerticalAlignment = VerticalAlignment.Center;
                            break;
                        case "Stretch":
                            NewPath.VerticalAlignment = VerticalAlignment.Stretch;
                            break;
                        case "Top":
                            NewPath.VerticalAlignment = VerticalAlignment.Top;
                            break;
                    }
                    break;
            }
        }
        return NewPath;
    }

And finally this code add The new path to the grid (In this case Simbolo.Path = null and posicion = PosicionSimbolo.Abajo

   public void DrawSimbol(ISimbolo simbolo)
    {
        if (!simbolo.Dibujado)
        {
            //Si no se especifica la propiedad path del simbolo, se procede a colorear la superficie 
            if (simbolo.Path == null)
            {
                if (simbolo.FillColor != null)
                {
                    Shape ThisShape = IdSuperficieToShape(simbolo.Superficie.Id);
                    if (ThisShape != null)
                        ThisShape.Fill = (SolidColorBrush)(new BrushConverter().ConvertFrom(simbolo.FillColor));
                }
            }
            else
            {
                try
                {
                    PosicionSimbolo posicion = SimbolTools.posicion(simbolo.DibujarEn);
                    ContadorSimbolos += 1;
                    switch (posicion)
                    {
                        case PosicionSimbolo.Abajo:
                            PosicionPanelInferior +=1;
                            string Nombre = string.Format("PanelInferior{0}", PosicionPanelInferior.ToString().Trim());
                            Path MyPath = SimbolTools.TextToPath(simbolo.Path, Nombre);
                            switch (PosicionPanelInferior)
                            {
                                case 1:
                                    PanelInferior1Contenedor.Children.Add(MyPath);
                                    break;
                                case 2:
                                    PanelInferior2Contenedor.Children.Add(MyPath);
                                    break;
                                case 3:
                                    PanelInferior3Contenedor.Children.Add(MyPath);
                                    break;
                                case 4:
                                    PanelInferior4Contenedor.Children.Add(MyPath);
                                    break;
                            }
                            MyPath.Visibility = System.Windows.Visibility.Visible;
                            break;
                    }
                }
                catch (Exception e)
                {
                    throw new ParadigmaNException(Errores.GRAMAS_NoSePudoDibujarSimbolo,e);
                }
            }
        }
    } 

I expect the two paths has the same shape but this is the result:

enter image description here

After that I have some Questions: 1- Why is the shape not the same? 2- There is a way to retrieve the XAML code for the path added by code? This is because I want to compare with the static XAML of the path from column2 3- What is the proper way to créate the path programmatically that match with the path from de column2?

Upvotes: 1

Views: 103

Answers (1)

user3455395
user3455395

Reputation: 161

XamlWriter can be used to dump the resulting XAML regardless of how it's been created.

Upvotes: 1

Related Questions