zoukankan      html  css  js  c++  java
  • WPF 之 Behavior、TriggerAction、TargetedTriggerAction

    这三种类都是用来扩展现有控件的行为。使用时需要引用System.Windows.Interactivity.dll。
    Behavior及TriggerAction都是用来扩展自身行为,而TargetdTriggerAction则是根据自身行为扩展目标行为。
    一个简单的使用例子如下(参见 http://www.codeproject.com/Tips/401707/Behavior-and-Trigger-in-WPF-Silverlight):

        public class MouseEnterBehavior: Behavior< TextBlock>
        {
            protected override void OnAttached()
            {
                this.AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
                this.AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
            }
    
            void AssociatedObject_MouseLeave( object sender, System.Windows.Input.MouseEventArgs e)
            {
                this.AssociatedObject.Background = Brushes.Transparent;
            }
    
            void AssociatedObject_MouseEnter( object sender, System.Windows.Input.MouseEventArgs e)
            {
                this.AssociatedObject.Background = Brushes.Aquamarine;
            }
    
            protected override void OnDetaching()
            {
                this.AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
                this.AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
            }
        }
    
        public class MouseEnterAction : TriggerAction<TextBlock >
        {
            protected override void Invoke( object parameter)
            {
                this.AssociatedObject.Text = "MouseEnterAction";
            }
        }
    
        public class TargetMouseEnterAction : TargetedTriggerAction<TextBlock >
        {
            protected override void Invoke( object parameter)
            {
                this.Target.Text = ( this.AssociatedObject as TextBlock).Text;
                this.Target.Background = ( this.AssociatedObject as TextBlock).Background;
            }
        }
           <TextBlock HorizontalAlignment ="Left" Height="17" Margin="32,39,0,0" TextWrapping="Wrap" Text="MouseEnterBehavior" VerticalAlignment ="Top" Width="124">
                <i: Interaction.Behaviors>
                    <behaviorSample: MouseEnterBehavior/>
                </i: Interaction.Behaviors>
                <i: Interaction.Triggers>
                    <i: EventTrigger EventName="MouseDown">
                        <behaviorSample: MouseEnterAction/>
                        <behaviorSample: TargetMouseEnterAction TargetName="AssociateTb"/>
                    </i: EventTrigger>
                </i: Interaction.Triggers>
            </TextBlock>
            <TextBlock x :Name="AssociateTb" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TargetTb" VerticalAlignment ="Top" Margin="32,71,0,0" Width="113"/>
    
  • 相关阅读:
    [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI
    Cookie对象实战
    怎么样修改地址栏前面的图标
    进程管理工具可以下载使用
    怎样才能实现表格背景图片拉伸
    TabControl控件的最佳实践,可以把一个窗体和用户控件添加进来
    flex+eclipse
    为gridview“删除”列添加确认对话框的方法
    触发器deleted 表和 inserted 表详解!!!
    ExtJs的使用
  • 原文地址:https://www.cnblogs.com/maigc249/p/5509872.html
Copyright © 2011-2022 走看看