csharp WPFDraggablePopup(ドラッグ可能なポップアップ)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WPFDraggablePopup(ドラッグ可能なポップアップ)相关的知识,希望对你有一定的参考价值。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:local="clr-namespace:DraggablePopup" x:Class="DraggablePopup.MainWindow"
        Title="MainWindow" Height="350" Width="525">
	<Window.Resources>
		<Storyboard x:Key="OnClick1">
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Popup.IsOpen)" Storyboard.TargetName="popup">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
			</BooleanAnimationUsingKeyFrames>
		</Storyboard>
	</Window.Resources>
	<Window.Triggers>
		<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
			<BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
		</EventTrigger>
	</Window.Triggers>
	<Grid>
		<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="40,45,0,0" VerticalAlignment="Top" Width="75"/>
		<Popup x:Name="popup" PopupAnimation="Scroll" AllowsTransparency="True" Placement="Center">
			<i:Interaction.Behaviors>
				<local:DragPopupBehavior/>
			</i:Interaction.Behaviors>
			<Grid Background="#FFE4A5A5" Height="100" Width="245">
				<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="25,32.644,0,0" TextWrapping="Wrap" Text="ポップアップ" VerticalAlignment="Top"/>
				<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="90,30.644,0,0" VerticalAlignment="Top" Width="75"/>
			</Grid>
		</Popup>
	</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;

namespace DraggablePopup
{
  /// <summary>
  /// 参考にしたURL:http://stackoverflow.com/questions/4784240/a-draggable-popup-control-in-wpf
  /// </summary>
  public class DragPopupBehavior : Behavior<Popup>
  {
    bool _mouseDown;
    Point _oldPosition;

    protected override void OnAttached()
    {
      base.OnAttached();

      AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
      AssociatedObject.MouseMove += AssociatedObject_MouseMove;
      AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
    }

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

      base.OnDetaching();
    }

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

    void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
      if (!_mouseDown)
      {
        return;
      }

      var newPosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
      var offset = (newPosition - _oldPosition);
      _oldPosition = newPosition;

      AssociatedObject.HorizontalOffset += offset.X;
      AssociatedObject.VerticalOffset += offset.Y;

    }

    void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      _mouseDown = true;
      _oldPosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
      AssociatedObject.Child.CaptureMouse();
    }
  }
}

以上是关于csharp WPFDraggablePopup(ドラッグ可能なポップアップ)的主要内容,如果未能解决你的问题,请参考以下文章

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp - 程序 - ConvertFilesToDifferentFormats.cs

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndex.cs