wpf WPF更改矩形填充代码的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24347954/
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 change color of Rectangle Fill in code
提问by StealthRT
Hey all I am new at WPF's and am in need of changing the color of a Rectangle fill in a WPF.
大家好,我是 WPF 的新手,需要更改 WPF 中矩形填充的颜色。
Currently I have this:
目前我有这个:
<Rectangle Fill="{Binding aColor}" RadiusY="5" RadiusX="5">
<Rectangle.Effect>
<DropShadowEffect ShadowDepth="0"/>
</Rectangle.Effect>
</Rectangle>
I am not sure how to go about using that binding above within code.
我不确定如何在代码中使用上面的绑定。
Any help would be great!
任何帮助都会很棒!
回答by Sajeetharan
Fill property is of type Brush , so you cant bind color directly,
Fill 属性是 Brush 类型,所以你不能直接绑定颜色,
Do something like this,
做这样的事情,
<Rectangle Width="100"
Height="100">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding color}"/>
</Rectangle.Fill>
</Rectangle>
Otherway is you can implement your Color-To-Brush converter. Like this
其他方式是您可以实现您的颜色到画笔转换器。像这样
回答by StealthRT
Got it!
知道了!
C#:
C#:
System.Drawing.Color c = ColorTranslator.FromHtml("#FFFFFF");
System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb(c.R, c.G, c.B);
aColor.Color = color;
XAML:
XAML:
<Rectangle RadiusY="5" RadiusX="5">
<Rectangle.Fill>
<SolidColorBrush x:Name="aColor"/>
</Rectangle.Fill>
<Rectangle.Effect>
<DropShadowEffect ShadowDepth="0"/>
</Rectangle.Effect>
</Rectangle>

