Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a cross in WPF

I have a WPF Control.

I need to have in background a cross, like this:
enter image description here

After that, I'd be able to add other controls over my "crossed" background: enter image description here

How should I draw the cross, knowing that when I rezize the control, the cross should follow its size?

like image 794
moldovanu Avatar asked Apr 13 '12 17:04

moldovanu


People also ask

How do you draw a line in WPF?

To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.

How do you draw a rectangle in WPF?

To draw a rectangle, create a Rectangle element and specify its Width and Height. To paint the inside of the rectangle, set its Fill. To give the rectangle an outline, use its Stroke and StrokeThickness properties. To give the rectangle rounded corners, specify the optional RadiusX and RadiusY properties.

How do you draw a circle in WPF?

Use its Stroke property to specify the Brush that is used to paint the outline of the ellipse. The StrokeThickness property specifies the thickness of the ellipse outline. To draw a circle, make the Width and Height of the Ellipse element equal to each other.

What is a viewbox in WPF?

The Viewbox control is used to stretch or scale a child element.


3 Answers

Quick and dirty way is to use lines and bind their coordinates to the width and height of some parent container. Something like this:

<Grid Name="parent">
    <Line  X1="0" Y1="0" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="{Binding ElementName='parent', Path='ActualHeight'}" 
           Stroke="Black" StrokeThickness="4" />
    <Line  X1="0" Y1="{Binding ElementName='parent', Path='ActualHeight'}" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="0" Stroke="Black" StrokeThickness="4" />
</Grid>

Using a grid as the parent means any other children added to the grid after the lines will appear on top of the lines:

<Grid Name="parent">
    <Line  X1="0" Y1="0" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="{Binding ElementName='parent', Path='ActualHeight'}" 
           Stroke="Black" StrokeThickness="4" />
    <Line  X1="0" Y1="{Binding ElementName='parent', Path='ActualHeight'}" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="0" Stroke="Black" StrokeThickness="4" />
    <Label Background="Red" VerticalAlignment="Center" HorizontalAlignment="Center">My Label</Label>
</Grid>
like image 114
Matt Burland Avatar answered Oct 11 '22 18:10

Matt Burland


Another way to solve this is to just put everything in a Viewbox and use Stretch="fill". It will handle re-sizing for you while maintaining the proper proportions. You won't need to use databinding in this case.

<Grid  HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
    <Viewbox HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Stretch="Fill">
        <Grid>
            <Line  X1="0" Y1="0" X2="100" Y2="100" Stroke="Black" StrokeThickness="1" />
            <Line  X1="0" Y1="100" X2="100" Y2="0" Stroke="Black" StrokeThickness="1" />
        </Grid>
    </Viewbox>
    <Label Background="Red" VerticalAlignment="Center" HorizontalAlignment="Center">My Label</Label>
</Grid>
like image 27
Bojin Li Avatar answered Oct 11 '22 18:10

Bojin Li


Rather than creating two elements for the layout engine, you can use a single Path item:

<Path Data="M0,0L16,16M16,0L0,16" Stroke="Black" />

This is a 16x16 "X" symbol:

enter image description here

You can specify a different size in the Data element, or use a ViewBox to scale it to its parent size if needed.

Note that the line ends are square, and slightly extend beyond the 16x16 square:

enter image description here

If you want the box to be exactly 16x16, you can set Width="16" Height="16" on the Path, which clips the line ends:

enter image description here

like image 38
Drew Noakes Avatar answered Oct 11 '22 19:10

Drew Noakes