WPF 为笔画和填充塑造不同的不透明度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33077166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF shape different opacity for stroke and fill
提问by Patrick
This is a very basic question. I want to be able to add a shape defining different opacity for fill and for stroke. If I add this:
这是一个非常基本的问题。我希望能够添加一个形状,为填充和描边定义不同的不透明度。如果我添加这个:
Ellipse e = new Ellipse();
e.Width = e.Height = 150;
e.Stroke = Brushes.Aqua;
e.Fill = Brushes.Chartreuse;
e.StrokeThickness = 20;
e.Opacity = .25;
plotCanvas.Children.Add(e);
I can only set 1 opacity. Instead I would like the fill to be 0.25 opaque and the stroke to be 1.0 opaque. Thank you Patrick
我只能设置 1 个不透明度。相反,我希望填充为 0.25 不透明,笔触为 1.0 不透明。谢谢帕特里克
回答by Willem van Rumpt
Setting the Opacity on the Ellipse will set the opacity for the entire control. What you want to do is create dedicated Brushes for Fill and Stroke, and control the opacity on the Brushes, i.e. :
在椭圆上设置不透明度将设置整个控件的不透明度。你想要做的是为填充和描边创建专用的画笔,并控制画笔的不透明度,即:
SolidColorBrush strokeBrush = new SolidColorBrush(Colors.Aqua);
strokeBrush.Opacity = .25d;
Alternatively, you could control the Alpha channel of the brush:
或者,您可以控制画笔的 Alpha 通道:
SolidColorBrush strokeBrush = new SolidColorBrush(Color.FromArgb(/*a, r, g, b*/));
回答by Mustafa Taleb
<Ellipse Stroke="Red" Width="200" Height="100" StrokeThickness="5">
<Ellipse.Fill>
<SolidColorBrush Color="Green" Opacity=".25"></SolidColorBrush>
</Ellipse.Fill>
</Ellipse>
Or in C# you can set the fill to a new SolidColorBrush with the desired opacity for the Opacity property.
或者在 C# 中,您可以将填充设置为新的 SolidColorBrush,并为 Opacity 属性设置所需的不透明度。
回答by Philip W
You can't set the opacity twice for a single Shape object. Insteaf of setting the opacity twice you can add a Border to your Ellipse:
您不能为单个 Shape 对象设置两次不透明度。代替设置不透明度两次,您可以向椭圆添加边框:
<Canvas x:Name="MyCanvas" Width="1000" Height="1000" Background="White">
<Border BorderBrush="Black" Opacity="1" BorderThickness="10" CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}">
<Ellipse Height="150" Width="150" Fill="Black" Opacity="0.25"></Ellipse>
</Border>
But since the Border is a rectangle which encloses the ellipse, you also need to set the cornerradius
但是由于 Border 是一个包含椭圆的矩形,因此您还需要设置拐角半径

