Skip to content

Instantly share code, notes, and snippets.

@devlights
Created May 25, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devlights/c0dbb7eba13be42dad5d to your computer and use it in GitHub Desktop.
Save devlights/c0dbb7eba13be42dad5d to your computer and use it in GitHub Desktop.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment