开发者

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

开发者 https://www.devze.com 2023-02-18 08:08 出处:网络
Say I have a rectangle wi开发者_如何学运维th a certain stroke color. Can I define certain edges to have different colors? For example, say I want the top and bottom of the stroke to be one color, but

Say I have a rectangle wi开发者_如何学运维th a certain stroke color. Can I define certain edges to have different colors? For example, say I want the top and bottom of the stroke to be one color, but the left and right of the stroke to be a different color?

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


I ended up doing this by having two borders one on top of eachother. And I adjust the border thicknesses accordingly.


Not out of the box. Also unfortunately both Rectangle and Border are sealed classes, so your best bet is to extend Shape class, implement a rectangle and create Brush dependency properties for each edges (with the default being the already existing Stroke Brush).

Edit: alternatively you can template this in XAML, just use a bunch of Borders on top of each other and only show 1 edge each.


In addition to what has already been said this cant be done with as-is controls but you could use Paths in a grid to get the same effect depending on what you want it 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>


You could accomplish this by using a more complex 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 isn't brilliant, and relies on the size of the border being known and fixed. However, there are other variants of this that may work better, using some of the other brush types.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号