开发者

SolidColorBrush color change at runtime

I have in my application resources file brush:

<SolidColorBrush x:Key="MainColor" Color="#FF15428B" /> 

I want to change color of this brush at runtime. I added color picker - when user choose color I want this brush to have selected color.

I tried code like that:

SolidColorBrush MainColor = new SolidColo开发者_StackOverflowrBrush(SelectedColor);

But it didn't work.


You need to set the existing brush's Color property.

You can get the instance by writing (SolidColorBrush)Resources["MainColor"]


You can access Resources from the code-behind with the TryFindResource method:

SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("myBrush");

if (myBrush != null)
{
    myBrush.Color = Colors.Yellow  ;
}


MainColor = new SolidColorBrush(Color.FromArgb(
    SelectedColor.A, SelectedColor.R, SelectedColor.G, SelectedColor.B
));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜