views:

2287

answers:

3

In Silverlight, how can I stretch the width of a Line to fill the width of the StackPanel in which it is a child element? Prefer a XAML solution, not a code-behind. Here is how I can do it in WPF:

<Line X1="0" X2="{Binding Path=ActualWidth, ElementName=HolePatternStackPanel}" Stroke="Gray" StrokeThickness="1" />

But that does not work in Silverlight.

+2  A: 

This was answered on Silverlight.net forum by user sladapter as follows:

Current Silverlight does not support element binding.

Do you have to use Line object? If you just want to draw a Line you can use Rectangle (2 px height) , then you can set HorizontalAlignment="Stretch" it so it will stretch automatically.

It worked peferctly for my needs.

MattSlay
+4  A: 
<Line X1="0" Y1="0" X2="0" Y2="1" Stretch="Fill" Stroke="#FF000000" />

Works in both WPF and Silverlight.

If the X's are 0, it's a vertical line. If the Y's are 0, it's horizontal. Use X1=0 Y1=0 X2=1 Y2=1 for diagonal from top left to bottom right, or reverse it for bottom left to top right.

Funky fresh.

headbiznatch
+1  A: 

ElementBinding does exist in Silverlight 4. But there's still not a great way to stretch an item to fill its parent container. What you really want is to fit into the parent container in the Visual Tree, which takes into consideration the application of templates.

Dan Vanderboom