[Silverlight] Behavior 實作 Blur 特效
2010/10/13
因為最近有人問到.. 希望我在做一個Behavior 範例...
前一篇分享過 在Silverlight中怎麼使用模糊(Blur)特效
之前有分享過一篇 使用Visual Studio.net + Microsoft Expression Blend 製作 Behavior
現在就把兩篇結合…
在 BehaviorLb 中開一個 BlurBehavior.cs
Code 如下(實作部分就不贅述,可參考 使用Visual Studio.net + Microsoft Expression Blend 製作 Behavior):
using System.Windows; using System.Windows.Interactivity; using System.Windows.Media.Effects; namespace BehaviorLib { public class BlurBehavior : Behavior<DependencyObject> { //預設一個BlurEffect BlurEffect effect = null; //預設一個模糊程度為10 public double _BlurRadius = 10; //開一個Property 做Binding public double BlurRadius { get { return _BlurRadius; } set { _BlurRadius = value; this.effect.Radius = value; } } //Ctor public BlurBehavior() { effect = new BlurEffect(); effect.Radius = BlurRadius; } protected override void OnAttached() { base.OnAttached(); //加入滑鼠移入以及移出事件 (this.AssociatedObject as UIElement).MouseEnter += MessageBehavior_MouseEnter; (this.AssociatedObject as UIElement).MouseLeave += MessageBehavior_MouseLeave; } void MessageBehavior_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) { //移除該作用的元件特效 (this.AssociatedObject as UIElement).Effect = null; } void MessageBehavior_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { //套入特效 (this.AssociatedObject as UIElement).Effect = effect; } protected override void OnDetaching() { base.OnDetaching(); //移除事件 (this.AssociatedObject as UIElement).MouseEnter -= MessageBehavior_MouseEnter; (this.AssociatedObject as UIElement).MouseLeave -= MessageBehavior_MouseLeave; } } }
其中 改的Code 不多…
值得一提的是.. BlurRadius 這Property ..
在Expression Blend 裡面 會結合IDE 這樣可以讓設計..溫馨的調數值..
有機會大家可以分享一下Behavior 應該頗好玩的…(茶~~
範例結果:
讚一下:
範例下載
標籤:
Silverlight
|
This entry was posted on 下午4:05
and is filed under
Silverlight
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
訂閱:
張貼留言 (Atom)
0 意見:
張貼留言