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

不想你离开。 提交于 2020-01-06 03:41:26

问题


Say I have a rectangle with 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?


回答1:


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




回答2:


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.




回答3:


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>



回答4:


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.



来源:https://stackoverflow.com/questions/5379307/wpf-silverlight-is-it-possible-to-have-a-different-stroke-color-for-each-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!