WPF / Silverlight: Is it possible to have a different stroke color for each edge?

Say I have a rectangle with a specific stroke color. Can I define specific edges for different colors? For example, let's say I want the top and bottom of the stroke to be the same color, but the colors to the left and right of the stroke are different?

If this is not possible, do you know about a good way?

+1
source share
4 answers

In the end, I did this, having two boundaries on top of each other. And I accordingly change the thickness of the border.

+2
source

Not out of the box. Unfortunately, both Rectangle and Border are sealed classes, so it’s best to extend the Shape class, implement a rectangle, and create Brush dependency properties for each edge (by default, this is an existing brush stroke).

Edit: Alternatively, you can create a template in XAML, just use the Bundle of Borders on top of each other and show only one edge.

+1
source

In addition to what has already been said, this cannot be done using as-a controls, but you can use Paths in the grid to get the same effect depending on what you want for.

<Grid Margin="5"> <Path Stroke="Red" StrokeThickness="1"> <Path.Data> <LineGeometry StartPoint="0 0" EndPoint="0 100"/> </Path.Data> </Path> <Path Stroke="Yellow" StrokeThickness="1"> <Path.Data> <LineGeometry StartPoint="0 100" EndPoint="100 100"/> </Path.Data> </Path> <Path Stroke="Pink" StrokeThickness="1"> <Path.Data> <LineGeometry StartPoint="100 100" EndPoint="100 0"/> </Path.Data> </Path> <Path Stroke="Green" StrokeThickness="1"> <Path.Data> <LineGeometry StartPoint="100 0" EndPoint="0 0"/> </Path.Data> </Path> </Grid> 
+1
source

You can accomplish this using a more sophisticated brush for your border:

 <Border BorderThickness="2" Width="200" Height="100"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0,0" EndPoint="0.5,0" SpreadMethod="Reflect"> <GradientStop Color="Blue" Offset="0" /> <GradientStop Color="Blue" Offset="0.02" /> <GradientStop Color="Red" Offset="0.02" /> </LinearGradientBrush> </Border.BorderBrush> </Border> 

This is not brilliant and depends on the size of the border and its definition. However, there are other variations of this that may work better using some other types of brushes.

0
source

All Articles