zoukankan      html  css  js  c++  java
  • WPF之行为(Behavior) 安静点

    行为

    行为的用法有些类似触发器的效果,但是触发器一般只能适用同一种的控件;而一个行为可以用在不同控件下(指定相同的父类);

    下面实现鼠标移入指定控件的时候发生高亮显示:

    namespace MyWpf.CommonService
    {
        /// <summary>
        /// FrameworkElement的派生类下的控件元素都会触发这个行为
        /// </summary>
        public class EffectBehavior : Behavior<FrameworkElement>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
                //关联鼠标移入移出的事件
                AssociatedObject.MouseMove += AssociatedObject_MouseMove;
                AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
            }
            /// <summary>
            /// 鼠标移出事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
            {
                var element = sender as FrameworkElement;
                element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 0 };
            }
            /// <summary>
            /// 鼠标移入事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
            {
                var element = sender as FrameworkElement;
                //鼠标移入时候的颜色设置
                element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Red, ShadowDepth = 0 };
            }
    
            /// <summary>
            /// 解除关联的事件
            /// </summary> 
            protected override void OnDetaching()
            {
                base.OnDetaching();
                AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
                AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
            }
    
    
    
        }
    }

    xaml:

    <UserControl x:Class="MyWpf.MyBehavior"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:MyWpf.CommonService"
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <Grid>
            <StackPanel>
                <TextBox Width="100" Height="30" Margin="40">
                    <i:Interaction.Behaviors>
                        <local:EffectBehavior/>
                    </i:Interaction.Behaviors>
                </TextBox>
                <Button Width="100" Height="30" Margin="40">
                    <i:Interaction.Behaviors>
                        <local:EffectBehavior/>
                    </i:Interaction.Behaviors>
                </Button>
            </StackPanel>
        </Grid> 
    </UserControl>

    结果:

      

  • 相关阅读:
    动态显示和隐藏状态栏(包括底部虚拟按键)
    android平台手电筒开发源代码
    实现IOS圆角风格的列表ListView
    在android中使用achartengine来绘制各种图表
    模拟iOS系统原生导航条隐藏或显示动画
    调整label中text显示的行间距
    Reinforcement Learning Using a Continuous Time Actor-Critic Framework with Spiking Neurons
    A neural reinforcement learning model for tasks with unknown time delays
    Event-driven Random Backpropagation: Enabling Neuromorphic Deep Learning Machines
    S4NN: temporal backpropagation for spiking neural networks with one spike per neuron
  • 原文地址:https://www.cnblogs.com/anjingdian/p/15605361.html
Copyright © 2011-2022 走看看