I have a WPF Control.
I need to have in background a cross, like this:
After that, I'd be able to add other controls over my "crossed" background:
How should I draw the cross, knowing that when I rezize the control, the cross should follow its size?
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.
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.
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.
The Viewbox control is used to stretch or scale a child element.
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>
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>
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:
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:
If you want the box to be exactly 16x16, you can set Width="16" Height="16"
on the Path
, which clips the line ends:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With