代码改变世界

Silverlight杂记-behaviors

2010-12-26 19:20  撞破南墙  阅读(603)  评论(0编辑  收藏  举报

 

使用已经存在的behaviors

1引入DLL

image

2在XAML中添加行为,

在这里添加了2个,一个是拖动,一个是缓动效果

<Rectangle x:Name="PurpleSquare" 
                Height
="20" 
                Width
="20" 
                HorizontalAlignment
="Left" 
                VerticalAlignment
="Top" 
                Margin
="20" 
                Fill
="BlueViolet"> 
        
<i:Interaction.Behaviors>
           
<ei:MouseDragElementBehavior> 拖拽效果行为
               
</ei:MouseDragElementBehavior>

            
<ei:FluidMoveBehavior Duration="0:0:4">缓动 
                
<ei:FluidMoveBehavior.EaseX> 
                    
<ElasticEase EasingMode="EaseOut" 
                                    Oscillations
="3" 
                                    Springiness
="4" /> 
                
</ei:FluidMoveBehavior.EaseX> 
                
<ei:FluidMoveBehavior.EaseY> 
                    
<ElasticEase EasingMode="EaseOut" 
                                    Oscillations
="3" 
                                    Springiness
="4" /> 
                
</ei:FluidMoveBehavior.EaseY> 
            
</ei:FluidMoveBehavior> 
        
</i:Interaction.Behaviors>

 

private void StartMove_Click(object sender, RoutedEventArgs e) { 
         Thickness margin 
= PurpleSquare.Margin; 
         margin.Left 
+= 100
         margin.Top 
+= 100;
         PurpleSquare.Margin 
= margin; 
     }

 

 

 

自定义behaviors

public class CustomBehavior : Behavior<Button>
  {
      protected override void OnAttached()
      {
          base.OnAttached();
          AssociatedObject.Click += new RoutedEventHandler(OnButtonClick);
      }

      protected override void OnDetaching()
      {
          base.OnDetaching();
          AssociatedObject.Click -= OnButtonClick;
      }

      void OnButtonClick(object sender, RoutedEventArgs e)
      {
          MessageBox.Show("自定义行为-!  MessageBox.Show");
      }
  }

image

 

 

源码下载

https://files.cnblogs.com/facingwaller/Behaviors.rar

 

扩展阅读

BLEND下的Behaviors

http://www.cnblogs.com/jv9/archive/2010/04/03/1703554.html