程序员的知识教程库

网站首页 > 教程分享 正文

26.WPF 行为(wpf hwnd)

henian88 2024-08-13 06:16:39 教程分享 19 ℃ 0 评论

摘要


  Expression Blend创作者开发了称为行为(behavior)的特征。其思想很简单:创建封装了一些通用用户界面功能的行为。这一功能可以是基本功能(如启动故事板或导航到超链接),也可以是复杂功能(如处理多点触摸交互,或构建使用实时物理引擎的碰撞模型)。一旦构建功能,就可将它们添加到任意应用程序的另一个控件中,具体方法是将该控件链接到适当的行为并设置行为的属性。在Expression Blend中,只通过拖放操作就可以使用行为。

正文


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

Nuget 安装System.Windows.Interactivity.dll Blend.Interctivity.WPF.v4.0

我们把以前的拖功能写成行为

<Window x:Class="_26.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_26"
        mc:Ignorable="d"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="250" Width="500">
    <Canvas>
        <Canvas Height="90" Width="90" Background="Bisque">
            <i:Interaction.Behaviors>
                <local:DragBehavior></local:DragBehavior>
            </i:Interaction.Behaviors>
        </Canvas>
        <Canvas Height="90" Width="90" Background="Red" Canvas.Left="135" Canvas.Top="27">
            <i:Interaction.Behaviors>
                <local:DragBehavior></local:DragBehavior>
            </i:Interaction.Behaviors>
        </Canvas>
        <TextBlock Height="30" Width="90" Background="DarkOrange" Text="拽的动">
            <i:Interaction.Behaviors>
                <local:DragBehavior></local:DragBehavior>
            </i:Interaction.Behaviors>
        </TextBlock>
    </Canvas>
</Window>

写一个行为类

public class DragBehavior:Behavior<UIElement>
{
    private Canvas canvas;
    private bool _isDragging;
    private Point _mouseOffset;

    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
        this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;
        this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
    }

    private void AssociatedObject_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (_isDragging)
        {
            AssociatedObject.ReleaseMouseCapture();
            _isDragging = false;
        }
    }

    private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (_isDragging)
        {
            Point point = e.GetPosition(canvas);
            AssociatedObject.SetValue(Canvas.TopProperty,point.Y-_mouseOffset.Y);
            AssociatedObject.SetValue(Canvas.LeftProperty,point.X-_mouseOffset.X);
        }
    }

    private void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (canvas == null)
        {
            canvas = VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas;
        }
        _isDragging = true;
        _mouseOffset=e.GetPosition(AssociatedObject);
        AssociatedObject.CaptureMouse();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
        this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
        this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
    }
}

一个背景样式行为

通过AssociatedObject(关联对象:是行为的关联对象,类型为我们指定的FrameworkElement),实现实际行为的触发:鼠标移入,背景高亮效果。

<Window x:Class="_26.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_26"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        mc:Ignorable="d"
        Title="Window1" Height="250" Width="500">
    <StackPanel Margin="10">
        <TextBox Margin="5">
            <i:Interaction.Behaviors>
                <local:EffectBehavior></local:EffectBehavior>
            </i:Interaction.Behaviors>
        </TextBox>
        <Button Content="Click" Margin="5">
            <i:Interaction.Behaviors>
                <local:EffectBehavior></local:EffectBehavior>
            </i:Interaction.Behaviors>
        </Button>
    </StackPanel>
</Window>

行为类

internal class EffectBehavior : Behavior<FrameworkElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
        AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
    }

    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 };
    }

    private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        var element = sender as FrameworkElement;
        element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Blue, ShadowDepth = 0 };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
        AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
    }
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表