以编程方式创建路径不适用于所需

时间:2014-03-24 14:10:14

标签: c# wpf xaml

我有这个自定义控件

        <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>

网格 PanelInferior 在运行时有4个地方可以绘制,如果我添加了另一个具有相同属性的路径,那么第2列就已经有了一个路径(仅用于测试)它不像其他人那样绘制(Column2)

这是传递给de TextToPath函数的字符串:

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"

这是在运行时创建路径的代码

   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;
    }

最后这段代码添加了网格的新路径(在这种情况下为Simbolo.Path = nullposicion = 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);
                }
            }
        }
    } 

我希望这两条路径具有相同的形状,但结果如下:

enter image description here

之后我有一些问题: 1-为什么形状不一样? 2-有一种方法可以检索代码添加的路径的XAML代码吗?这是因为我想与column2的路径的静态XAML进行比较 3-以编程方式编写与de column2路径匹配的路径的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

XamlWriter可用于转储生成的XAML,无论它是如何创建的。