美文网首页
Behavior的使用

Behavior的使用

作者: 雪之梦_8f14 | 来源:发表于2019-06-28 13:36 被阅读0次

    Behavior

    • Behavior将某些特定的功能从某个特定的控件中解耦出来,重用性高

    利用behavior实现以下功能

    • 点击某个控件 正常产生某个控件的事件
    • 点击某个控件并移动鼠标, 拖动整个窗口
    • 效果图如下


      dragwindow.gif
    • 源码
    • 相关连接

    代码

    • 定义一个类 继承抽象类 Behavior<T>(包含在System.Windows.Interactivity.dll中)
    • 重写两个方法 OnAttached 和 OnDetaching 获取一个宿主控件对象AssociatedObject
    • 以下是behavior的代码
    public class WinDragMoveBehavior: Behavior<Window>
        {
            #region Fields
    
            private Point _downPoint;
    
            #endregion Fields
    
            #region Properties
    
            public Window AttachedWindow { get; set; }
    
            #endregion Properties
    
            #region Methods
    
            protected override void OnAttached()
            {
                base.OnAttached();
                AttachedWindow = AssociatedObject as Window;
    
                if (AttachedWindow == null)
                {
                    return;
                }
    
                RegisterEvents();
            }
    
            protected override void OnDetaching()
            {
                base.OnDetaching();
    
                UnRegisterEvents();
            }
    
            /// <summary>
            ///  注册事件
            /// </summary>
            private void RegisterEvents()
            {
                if (AttachedWindow != null)
                {
                    AttachedWindow.PreviewMouseLeftButtonDown += AttachedGrid_PreviewMouseLeftButtonDown;
                    AttachedWindow.PreviewMouseMove += AttachedGridOnPreviewMouseMove;
                    AttachedWindow.PreviewMouseLeftButtonUp += AttachedGridOnPreviewMouseLeftButtonUp;
                }
            }
    
            /// <summary>
            /// 注销事件
            /// </summary>
            private void UnRegisterEvents()
            {
                if (AttachedWindow != null)
                {
                    AttachedWindow.MouseLeftButtonDown -= AttachedGrid_PreviewMouseLeftButtonDown;
                    AttachedWindow.MouseMove -= AttachedGridOnPreviewMouseMove;
                    AttachedWindow.MouseLeftButtonUp -= AttachedGridOnPreviewMouseLeftButtonUp;
                }
            }
    
            #endregion Methods
    
            #region EventHandlers
    
            private void AttachedGrid_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                _downPoint = e.GetPosition(AttachedWindow);
            }
    
            private void AttachedGridOnPreviewMouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    var mouseMovePoint = e.GetPosition(AttachedWindow);
    
                    if (AppUtils.IsAbleToDrag(mouseMovePoint, _downPoint))
                    {
                        AttachedWindow.DragMove();
                    }
                }
            }
    
            private void AttachedGridOnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                var mouseMovePoint = e.GetPosition(AttachedWindow);
    
                if (AppUtils.IsAbleToDrag(mouseMovePoint, _downPoint))
                {
                    e.Handled = true;
    
                    var element = Mouse.Captured;
                    element?.ReleaseMouseCapture();
                }
            }
    
            #endregion EventHandlers
        }
    
    • 使用此behavior
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
     xmlns:behavior="clr-namespace:DragAndClickWinSamples.Behaviors"
    <i:Interaction.Behaviors>
            <behavior:WinDragMoveBehavior></behavior:WinDragMoveBehavior>
    </i:Interaction.Behaviors>
    

    相关文章

      网友评论

          本文标题:Behavior的使用

          本文链接:https://www.haomeiwen.com/subject/bdpscctx.html