WPFDraggablePopup (ドラッグ可能なポップアップ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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