将XAML代码转换为C#代码

时间:2015-02-16 12:07:52

标签: c# wpf xaml converter

如何将此代码部分从XAML转换为C#代码?

 <ComboBoxItem  x:Name="cmbItemDashDot1">
            <Viewbox>
                <Image  Height="18" Width="70">
                    <Image.Source>
                        <DrawingImage>
                            <DrawingImage.Drawing>
                                <GeometryDrawing Brush="Black">
                                    <GeometryDrawing.Geometry>
                                        <LineGeometry StartPoint="0,9" EndPoint="38,9" />
                                    </GeometryDrawing.Geometry>
                                    <GeometryDrawing.Pen>
                                        <Pen Brush="Black"  Thickness="1"  DashStyle="{x:Static DashStyles.DashDot}"/>
                                    </GeometryDrawing.Pen>
                                </GeometryDrawing>
                            </DrawingImage.Drawing>
                        </DrawingImage>
                    </Image.Source>
                </Image>
            </Viewbox>
        </ComboBoxItem>

我找不到某些元素的类比。 或者我如何以编程方式在ComboBoxItem中绘制一条线?

1 个答案:

答案 0 :(得分:1)

试试此代码

     Image img = new Image();

     GeometryDrawing gDrwing = new GeometryDrawing();
     gDrwing.Brush = Brushes.Black;

     LineGeometry lineGeo = new LineGeometry();
     lineGeo.StartPoint = new Point(0, 9);
     lineGeo.EndPoint = new Point(38, 9);


     Pen pen = new Pen();
     pen.Brush = Brushes.Black;
     pen.Thickness = 1;
     pen.DashStyle = DashStyles.DashDot;

     gDrwing.Geometry = lineGeo;
     gDrwing.Pen = pen;

     DrawingImage geometryImage = new DrawingImage(gDrwing);

     img.Source = geometryImage;
     Viewbox vb = new Viewbox();
     vb.Child = img;

     comboBox1.Items.Add(vb);