■ Behavior<T> 클래스를 사용해 팝업을 드래그해서 이동하는 방법을 보여준다.
▶ PopupDragBehavior.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System.Windows.Controls; using System.Windows.Controls.Primitives; using Microsoft.Xaml.Behaviors; namespace TestProject { /// <summary> /// 팝업 드래그 동작 /// </summary> public class PopupDragBehavior : Behavior<Popup> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탈착시 처리하기 - OnAttached() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnAttached() { if(AssociatedObject.Child != null) { if(AssociatedObject.Child is Panel) { Thumb thumb = new Thumb() { Width = 0, Height = 0 }; Panel panel = AssociatedObject.Child as Panel; if(panel != null) { panel.Children.Add(thumb); } AssociatedObject.MouseDown += (s, e) => { thumb.RaiseEvent(e); }; thumb.DragDelta += (s, e) => { AssociatedObject.HorizontalOffset += e.HorizontalChange; AssociatedObject.VerticalOffset += e.VerticalChange; }; } } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Popup Placement="Center" IsOpen="True"> <StackPanel Background="Black"> <Label HorizontalAlignment="Center" Foreground="White" Content="팝업 드래그 테스트" /> <Grid Width="300" Height="150" Background="Blue"> </Grid> </StackPanel> <b:Interaction.Behaviors> <local:PopupDragBehavior /> </b:Interaction.Behaviors> </Popup> </Grid> </Window> |