绘制 WPF 形状,作为矩形:如果 Stroke 是透明的,则 StrokeThickness 减半 [英] Drawing a WPF shape, as a Rectangle: StrokeThickness halve if Stroke is Transparent

查看:47
本文介绍了绘制 WPF 形状,作为矩形:如果 Stroke 是透明的,则 StrokeThickness 减半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一些 UCControl 而不是设计几何形状.我可以在运行时配置尺寸(大小和笔触粗细)、颜色(背景和笔触),并且在我使用纯色之前一切正常.如果我使用透明笔刷笔画,会出现问题:形状以正确的尺寸和颜色出现,但笔画厚度减半.

the problem: I have some UCControl than design geometric shapes. I can configure at runtime, dimensions (size and stroke thickness), colours (background and stroke) and all work fine until I use solid colors. Problems happened if I use, for stroke, Transparent brush: the shape appears with correct dimensions and colours, but the strokethikness is halve.

<Grid x:Name="_grid" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Rectangle Grid.Row="0" Grid.Column="0" Margin="0,0,0,0"
        Width="{Binding ActualWidth, ElementName=_grid}"
        Height="{Binding ActualHeight, ElementName=_grid}" 
        Stroke="{Binding Rectangle.BorderColorBrush}"
        StrokeThickness="{Binding Rectangle.Thick}" 
        Fill="{Binding Rectangle.BackgroundBrush}"/>

</Grid>

我需要比笔画是纯色或透明的笔画粗的笔画是相同的.我此时发现:棕色是背景色,黑色或透明笔触.两者的 StrokeThickness 均为 20(参见点网格:距离为 10)

I need than if stroke is a solid color or Transparent, the stroke thick on the draw is the same. I found at this moment this: the Brown is background color, Black or Transparent the Stroke. StrokeThickness is 20 for both (see dot grid: distanced 10)

推荐答案

在这种特殊情况下,我需要使用 StrokeThickness 作为形状周围的边框,如果我想要边框大小为 20 我需要在形状周围的边框为 20, 颜色是纯色还是透明都没有关系,并且形状的总大小(边框 + 背景)不得发生变异.因此,考虑到如何绘制 Stroke(它对形状进行内部偏移,针对 StrokeThickness 的一半,在内部绘制一半笔划,在此偏移量之外绘制一半笔划),如果笔划颜色是透明的,我只需要复制 StrokeThickness.是的,这有点令人困惑,无论如何我希望它清楚且有用......这里的代码仅用于 Rectangle.StrokeThickness 元素:

In this particular case, I need to use StrokeThickness as a border around the shape, if I want border size 20 I need all around the shape a border of 20, doesn't matter if the color is a solid color, or Transparent, and the total size of shape (border + background) must not mutate. So, considering how the Stroke is drawed (it make an internal offset of the shape, for half of StrokeThickness, and draw half stroke inside, and half stroke outside this offset) I need simply to duplicate the StrokeThickness if the stroke color is Transparent. Yes, it's a little bit confusing, I hope it be clear and useful anyway... here the code just for Rectangle.StrokeThickness element:

<Rectangle.StrokeThickness>
    <MultiBinding Converter="{StaticResource MyConverter}">
        <Binding Path="Rectangle.Thick"></Binding>
        <Binding Path="Rectangle.BorderColorBrush"></Binding>
    </MultiBinding>
</Rectangle.StrokeThickness>

..and 转换器只是复制 Rectangle.Thick 值如果(Rectangle.BorderColorBrush == Brushes.Transparent).最终结果正是我想要的:

..and converter just duplicate Rectangle.Thick value if (Rectangle.BorderColorBrush == Brushes.Transparent). the final result is what I wanted:

这篇关于绘制 WPF 形状,作为矩形:如果 Stroke 是透明的,则 StrokeThickness 减半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆