Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In pure XAML, is it possible to get a Line to align to part of a Grid?

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!

like image 354
Jeff Wilcox Avatar asked Mar 12 '10 05:03

Jeff Wilcox


People also ask

How does margin work in XAML?

XAML ValuesMargin="20,50" will be interpreted to mean a Thickness with Left and Right set to 20, and Top and Bottom set to 50. The default unit for a Thickness measure is device-independent unit (1/96th inch). You can also specify other units by appending the unit type strings cm , in , or pt to any measure.

Which panel in WPF is well suited to place elements in an ordered and aligned manner?

The Panel ClassDerived Panel elements are used to position and arrange elements in Extensible Application Markup Language (XAML) and code.

How do I set margins in XAML?

The Margin property of UIElement, which is parent class of all WPF controls is used to set the margin of a control. Margin property takes four numbers - Left, Top, Right and Bottom, that is margin to the left top and right bottom. This example sets Margin of a Button control in XAML at design-time.

How do you draw a horizontal line in XAML?

Creating a Line The Line element in XAML creates a line shape. The following code snippet creates a Line by setting its start point (X1, Y1) to (50, 50) and end point (X2, Y2) to (200, 200). That means a line is drawn from point (50, 50) to (200, 200). The code also sets the color of the line to red and width 4.


1 Answers

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"/>
like image 103
Gabe Avatar answered Oct 15 '22 15:10

Gabe