I need to create a line with the first point centered to the Grid it is contained in (without manually setting Width/Height of Grid). Here is very simple code sample I want to update so I have one point of “Line1” centered within “Grid1”.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SimpleLine.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<Grid Name="Grid1">
<Line Name="Line1" X2="200" Y2="100" Stroke="Black"></Line>
</Grid>
</Grid>
</Window>
When I try in code behind:
Line1.X1 = Grid1.Widht/2; Line1.Y1 = Grid1.Height/2;
I get an error (uncaught exception) – that “Not a number” is not a valid value for X1(Y1).
Thanks for any efforts.
PS: I am rather a WPF-beginner.
In code behind you will have to use ActualWidth and ActualHeight if you have not explicitly set the Width/Height.
if you want to do in XAML you can make BindingConverters to perform logic on a binded value.
Xaml:
Binding converter:
This will take the Binded value (ActualWidth) and divide it by two and the same for ActualHeight.
So even if your from is resized the line will stay in the center