Draw a cross in WPF
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>
The answer of Matt Burland made my app constantly blinking (cause I suppose the reference to 'parent' resized it ... and then resized the lines, etc ...)
So I used Stretch=Fill and suppressed the reference to 'parent'. Works pretty fine now.
<Line x:Name="Line1" Visibility="Hidden" Stroke="Red" StrokeThickness="2" Stretch="Fill"
X1="0" Y1="0" X2="1" Y2="1" />
<Line x:Name="Line2" Visibility="Hidden" Stroke="Red" StrokeThickness="2" Stretch="Fill"
X1="0" Y1="1" X2="1" Y2="0" />
This is a mix from this solution and this one