0

How can I convert this code part from XAML to C# code?

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

I can not find analogies for some elements. Or How can I draw a line in ComboBoxItem programmatically?

2
  • 2
    Check that your project has a reference to System.Windows.Media Commented Feb 16, 2015 at 12:12
  • 2
    Can not find analogies - what is that? Do you miss namespaces? ComboBoxItem is System.Windows.Controls.ComboBoxItem (or just add using System.Windows.Controls;). If problem is that, then you can use google to find in which namespace is control (type "wpf controlname"). Or just right click ComboBoxItem text in code and choose Resolve and intellisense will fix it for you. Commented Feb 16, 2015 at 12:21

1 Answer 1

1

Try this code

     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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.