views:

173

answers:

3

Is it possible to create a Line in XAML (without any C# code behind) to align a line inside of a layout container such as a Grid?

I'd like to effectively have:

<Grid>
    <Line StrokeThickness="1" 
          HorizontalAlignment="Stretch" 
          VerticalAlignment="Bottom" 
          Stroke="Red"/>
</Grid>

I need to use StrokeDashArray and StrokeDashOffset, otherwise I would just use a Border control with the BorderThickness set to "0,0,0,1"...

Thanks for any ideas!

A: 

I think you need to use Path like this

<Grid x:Name="LayoutRoot" Background="White">

<Path Fill="Red" Stretch="Fill" Stroke="Black" StrokeDashArray="1" Height="4" Margin="8,0,7,7" VerticalAlignment="Bottom" UseLayoutRounding="False" Data="M8,127 L457,127" StrokeThickness="13"/>

</Grid>

Hope this Helps. Expression Blend is a must have for this kind of Challenges or even VS 2010 RC1 (For now)

kanchirk
A: 

How about this?

<Line x:Name="line" 
StrokeThickness="1"  
HorizontalAlignment="Stretch"  
VerticalAlignment="Bottom"  
Stroke="Red" 
X2="{Binding ActualWidth, ElementName=line, Mode=OneWay}"
Stretch="Fill" 
StrokeStartLineCap="Square"
StrokeEndLineCap="Square"/> 
Gongdo
Binding is interesting, thanks. For performance reasons though I'm weary to use it.
Jeff Wilcox
This code doesn't work for me.
Gabe
@gabe, you right. I missed Stretch, StrokeStartLineCap and StrokeEndLineCap. Now it'll be fine. Anyway, Your solution is better for all. :D
Gongdo
+1  A: 

To elaborate on kanchirk's response, this works for me:

<Path StrokeThickness="1"
 HorizontalAlignment="Stretch"  
 VerticalAlignment="Bottom"
 Data="M0,0 L1,0"
 Stretch="Fill"
 StrokeEndLineCap="Square"
 StrokeStartLineCap="Square"
 Stroke="Red"/> 

You can also the same thing with a Line:

<Line StrokeThickness="1" 
 HorizontalAlignment="Stretch"   
 VerticalAlignment="Bottom" 
 X2="1" 
 Stretch="Fill" 
 StrokeEndLineCap="Square" 
 StrokeStartLineCap="Square" 
 Stroke="Red"/>
Gabe
Thanks - it's pretty good, and does answer my question as phrased. Marking as answered.However, I really want to use this to mimic fancy underlining for a text block, so I don't want it to stretch larger than it needs to to fill the container.
Jeff Wilcox
Are you saying it will become wider than its container? Perhaps you should create a more specific question (and link to it from here) if this won't actually work for you.
Gabe