Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to draw a horizontal line without specifying X2??

5 views
Skip to first unread message

John Taylor

unread,
Aug 25, 2007, 10:22:12 PM8/25/07
to
I want to draw a horizontal line that fills up the entire available width of
a grid cell. I don't want to explicitly specify the line's width (X2). How
do I do this???

<Line Grid.Row="1" Stroke="Gray" StrokeThickness="1" X1="0" X2="200" Y1="1"
Y2="1" />

in above sample, I want to remove the "200" so that the line sizes
automatically when the grid is resized. thanks!

Laurent Bugnion, MVP

unread,
Aug 26, 2007, 5:35:12 PM8/26/07
to
Hi,

X2 is a DependencyProperty, so you can easily bind to the desired value.
For example:

<Grid x:Name="MyGrid">

<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Line Grid.Row="1"
X1="0"
X2="{Binding ElementName=MyGrid, Path=ActualWidth}"
Stroke="Red"
StrokeThickness="5"


Y1="1"
Y2="1"/>

</Grid>

Note: You must bind to ActualWidth, not to Width. ActualWidth is always
accurate, while Width is what the user entered, so the value may be Auto
or simply not set.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

John Taylor

unread,
Aug 26, 2007, 6:56:34 PM8/26/07
to
This doesn't seem to work when the grid is resized. Is there anything else I
should be doing?


"Laurent Bugnion, MVP" <galas...@bluewin.ch> wrote in message
news:#ett$jC6HH...@TK2MSFTNGP02.phx.gbl...

Laurent Bugnion, MVP

unread,
Aug 27, 2007, 2:36:54 AM8/27/07
to
Hi,

John Taylor wrote:
> This doesn't seem to work when the grid is resized. Is there anything
> else I should be doing?

It works fine here. Are you sure you copied my code? The whole example is:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid x:Name="MyGrid">

<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Line Grid.Row="1"
X1="0"
X2="{Binding ElementName=MyGrid, Path=ActualWidth}"
Stroke="Red"
StrokeThickness="5"
Y1="1"
Y2="1"/>

</Grid>
</Window>

If you copy this in KaXaml or in XamlPad, you'll see it works.

Greetings,
Laurent

jeffpierson

unread,
Oct 6, 2009, 8:29:00 AM10/6/09
to
The following works for me.

<Line
Grid.Row="2"
Stretch="Fill"
Stroke="Black"
X2="1" />

John Taylor wrote:

How to draw a horizontal line without specifying X2??
25-Aug-07

EggHeadCafe - Software Developer Portal of Choice
Convert XML ADO recordset to Table with the DOM
http://www.eggheadcafe.com/tutorials/aspnet/bf4a89cc-abe4-4f0d-9d8c-382c0c6335ad/convert--xml-ado-recordse.aspx

0 new messages