如何使用XAML定义的画笔资源,从C# [英] How to use a defined brush resource in XAML, from C#

查看:133
本文介绍了如何使用XAML定义的画笔资源,从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有这个

 < UserControl.Resource> 
<一个LinearGradientBrush X:键=KeyDownBrush.....>

现在,我想访问该定义的资源,当一个键被按下,并取代目前的对象填充在KeyDownBrush,在C#。



我试过this.Resource.Contains(KeyDownPress),并已经能够得到真正的恢复,所以我推测,我几乎没有但我不知道如何访问对象,并正确解析到一个刷机实例。



任何指导,将不胜感激。


< DIV CLASS =h2_lin>解决方案

从你的用户控件中:

  VAR刷=这一点。资源[KeyDownBrush]作为一个LinearGradientBrush; 



应该做的伎俩。


So far I have this

<UserControl.Resource>
 <LinearGradientBrush x:Key="KeyDownBrush" .....>

Now I would like to access this defined resource when a key is pressed and replace the current objects fill with the KeyDownBrush, in C#.

I've tried this.Resource.Contains("KeyDownPress") and have been able to get True returned so I presume I am almost there but I'm not sure how to access the object and Parse it correctly to a Brush instance.

Any guidance would be appreciated.

解决方案

From within your UserControl:

var brush = this.Resources["KeyDownBrush"] as LinearGradientBrush;

Should do the trick.

这篇关于如何使用XAML定义的画笔资源,从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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