■ UIElement 클래스의 MouseMove/DragEnter/DragLeave/DragOver/Drop 이벤트를 사용해 드래그 & 드롭을 하는 방법을 보여준다.
▶ 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 34 35 36 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Ellipse Name="sourceEllipse" Grid.Row="0" Grid.Column="0" Height="200" Width="200" Fill="Green" /> <Ellipse Name="targetEllipse" Grid.Row="0" Grid.Column="1" Height="200" Width="200" Fill="Blue" AllowDrop="True" /> <Button Name="resetButton" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="0 10 0 0" Width="100" Height="30" Content="재설정" /> </Grid> </Window> |
▶ MainWindow.xaml.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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이전 채우기 브러시 /// </summary> private Brush previousTargetBrush = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.sourceEllipse.MouseMove += sourceEllipse_MouseMove; this.targetEllipse.DragEnter += targetEllipse_DragEnter; this.targetEllipse.DragLeave += targetEllipse_DragLeave; this.targetEllipse.DragOver += targetEllipse_DragOver; this.targetEllipse.Drop += targetEllipse_Drop; this.resetButton.Click += resetButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 소스 타원 마우스 이동시 처리하기 - sourceEllipse_MouseMove(sender, e) /// <summary> /// 소스 타원 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sourceEllipse_MouseMove(object sender, MouseEventArgs e) { Ellipse sourceEllipse = sender as Ellipse; if(sourceEllipse != null && e.LeftButton == MouseButtonState.Pressed) { DragDrop.DoDragDrop(sourceEllipse, sourceEllipse.Fill.ToString(), DragDropEffects.Copy); } } #endregion #region 타겟 타원 드래그 진입시 처리하기 - targetEllipse_DragEnter(sender, e) /// <summary> /// 타겟 타원 드래그 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetEllipse_DragEnter(object sender, DragEventArgs e) { Ellipse targetEllipse = sender as Ellipse; if(targetEllipse != null) { this.previousTargetBrush = targetEllipse.Fill; if(e.Data.GetDataPresent(DataFormats.StringFormat)) { string sourceData = (string)e.Data.GetData(DataFormats.StringFormat); BrushConverter converter = new BrushConverter(); if(converter.IsValid(sourceData)) { Brush sourceBrush = (Brush)converter.ConvertFromString(sourceData); targetEllipse.Fill = sourceBrush; } } } } #endregion #region 타겟 타원 드래그 OVER 처리하기 - targetEllipse_DragOver(sender, e) /// <summary> /// 타겟 타원 드래그 OVER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetEllipse_DragOver(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; if(e.Data.GetDataPresent(DataFormats.StringFormat)) { string sourceData = (string)e.Data.GetData(DataFormats.StringFormat); BrushConverter converter = new BrushConverter(); if(converter.IsValid(sourceData)) { e.Effects = DragDropEffects.Copy | DragDropEffects.Move; } } } #endregion #region 타겟 타원 드래그 이탈시 처리하기 - targetEllipse_DragLeave(sender, e) /// <summary> /// 타겟 타원 드래그 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetEllipse_DragLeave(object sender, DragEventArgs e) { Ellipse targetEllipse = sender as Ellipse; if(targetEllipse != null) { targetEllipse.Fill = this.previousTargetBrush; } } #endregion #region 타겟 타원 드롭시 처리하기 - targetEllipse_Drop(sender, e) /// <summary> /// 타겟 타원 드롭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetEllipse_Drop(object sender, DragEventArgs e) { Ellipse targetEllipse = sender as Ellipse; if(targetEllipse != null) { if(e.Data.GetDataPresent(DataFormats.StringFormat)) { string sourceData = (string)e.Data.GetData(DataFormats.StringFormat); BrushConverter converter = new BrushConverter(); if(converter.IsValid(sourceData)) { Brush sourceBrush = (Brush)converter.ConvertFromString(sourceData); targetEllipse.Fill = sourceBrush; } } } } #endregion #region 재설정 버튼 클릭시 처리하기 - resetButton_Click(sender, e) /// <summary> /// 재설정 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void resetButton_Click(object sender, RoutedEventArgs e) { this.targetEllipse.Fill = Brushes.Blue; } #endregion } } |