Dreamine.UI.Wpf.Controls 1.0.1
Dreamine.UI.Wpf.Controls 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineImage.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Linq;
3using System.Windows;
4using System.Windows.Controls;
5using System.Windows.Input;
6using System.Windows.Media;
7
8using Dreamine.MVVM.Core;
9
11{
20 public class DreamineImage : Control
21 {
22
23 // Control 클래스에는 이미 BorderThickness, BorderBrush가 있습니다.
24 // CornerRadius는 Control에 없으므로 직접 추가합니다.
33 public static readonly DependencyProperty CornerRadiusProperty =
34 DependencyProperty.Register(
35 nameof(CornerRadius),
36 typeof(CornerRadius),
37 typeof(DreamineImage),
38 new FrameworkPropertyMetadata(new CornerRadius(0)));
39
40 // Image의 Source와 같은 역할을 할 프로퍼티를 새로 정의합니다.
49 public static readonly DependencyProperty SourceProperty =
50 DependencyProperty.Register(
51 nameof(Source),
52 typeof(ImageSource),
53 typeof(DreamineImage),
54 new FrameworkPropertyMetadata(null));
55
64 public static readonly DependencyProperty FallbackSourceProperty =
65 DependencyProperty.Register(
66 nameof(FallbackSource),
67 typeof(ImageSource),
68 typeof(DreamineImage),
69 new FrameworkPropertyMetadata(null));
70
79 public static readonly DependencyProperty ClickCommandProperty =
80 DependencyProperty.Register(
81 nameof(ClickCommand),
82 typeof(ICommand),
83 typeof(DreamineImage),
84 new PropertyMetadata(null));
85
94 public static readonly DependencyProperty CommandParameterProperty =
95 DependencyProperty.Register(
96 nameof(CommandParameter),
97 typeof(object),
98 typeof(DreamineImage),
99 new PropertyMetadata(null));
100
109 public static readonly DependencyProperty IsWindowDragEnabledProperty =
110 DependencyProperty.Register(
111 nameof(IsWindowDragEnabled),
112 typeof(bool),
113 typeof(DreamineImage),
114 new PropertyMetadata(false));
115
124 public static readonly DependencyProperty IsDoubleClickToggleMaximizeProperty =
125 DependencyProperty.Register(
127 typeof(bool),
128 typeof(DreamineImage),
129 new PropertyMetadata(true));
130
131 // Stretch 속성도 Image에서 가져와서 새로 정의합니다.
140 public static readonly DependencyProperty StretchProperty =
141 DependencyProperty.Register(
142 nameof(Stretch),
143 typeof(Stretch),
144 typeof(DreamineImage),
145 new PropertyMetadata(Stretch.Uniform));
146
156 {
157 get => (CornerRadius)GetValue(CornerRadiusProperty);
158 set => SetValue(CornerRadiusProperty, value);
159 }
160
161 // 새로 추가한 Source 프로퍼티
170 public ImageSource? Source
171 {
172 get => (ImageSource?)GetValue(SourceProperty);
173 set => SetValue(SourceProperty, value);
174 }
175
184 public ImageSource? FallbackSource
185 {
186 get => (ImageSource?)GetValue(FallbackSourceProperty);
187 set => SetValue(FallbackSourceProperty, value);
188 }
189
198 public ICommand? ClickCommand
199 {
200 get => (ICommand?)GetValue(ClickCommandProperty);
201 set => SetValue(ClickCommandProperty, value);
202 }
203
212 public object? CommandParameter
213 {
214 get => GetValue(CommandParameterProperty);
215 set => SetValue(CommandParameterProperty, value);
216 }
217
227 {
228 get => (bool)GetValue(IsWindowDragEnabledProperty);
229 set => SetValue(IsWindowDragEnabledProperty, value);
230 }
231
241 {
242 get => (bool)GetValue(IsDoubleClickToggleMaximizeProperty);
243 set => SetValue(IsDoubleClickToggleMaximizeProperty, value);
244 }
245
255 {
256 get => (Stretch)GetValue(StretchProperty);
257 set => SetValue(StretchProperty, value);
258 }
259
269 {
270 try
271 {
272
273 DefaultStyleKeyProperty.OverrideMetadata(typeof(DreamineImage),
274 new FrameworkPropertyMetadata(typeof(DreamineImage)));
275
276 var uri = new Uri("/Dreamine.UI.Wpf.Themes;component/DreamineImageStyle.xaml", UriKind.RelativeOrAbsolute);
277 bool alreadyAdded = Application.Current.Resources.MergedDictionaries
278 .OfType<ResourceDictionary>()
279 .Any(x => x.Source != null && x.Source.Equals(uri));
280
281 if (!alreadyAdded)
282 {
283 var dict = new ResourceDictionary { Source = uri };
284 Application.Current.Resources.MergedDictionaries.Add(dict);
285 }
286
287 }
288 catch
289 {
290 // 디자이너/테스트 환경 보호
291 }
292 }
293
303 {
304 PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;
305 MouseLeftButtonUp += OnMouseLeftButtonUp;
306 MouseMove += OnMouseMove;
307 // WPF Image에는 MouseDoubleClick 이벤트가 없으므로 사용하지 않음.
308 }
309
318 private Point _pressPoint;
327 private bool _isDragging;
328
353 private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
354 {
355 // 1) 더블클릭 토글 (WPF는 ClickCount로 판별)
356 if (IsDoubleClickToggleMaximize && e.ClickCount == 2)
357 {
358 var win = Window.GetWindow(this);
359 if (win != null)
360 {
361 try
362 {
363 win.WindowState = win.WindowState == WindowState.Maximized
364 ? WindowState.Normal
365 : WindowState.Maximized;
366 e.Handled = true;
367 return;
368 }
369 catch { /* 무시 */ }
370 }
371 }
372
373 // 2) 드래그 대비 기준점 기록
374 _pressPoint = e.GetPosition(this);
375 _isDragging = false;
376 CaptureMouse();
377 }
378
411 private void OnMouseMove(object sender, MouseEventArgs e)
412 {
413 if (!IsWindowDragEnabled || e.LeftButton != MouseButtonState.Pressed)
414 return;
415
416 var p = e.GetPosition(this);
417 var dx = Math.Abs(p.X - _pressPoint.X);
418 var dy = Math.Abs(p.Y - _pressPoint.Y);
419
420 if (!_isDragging &&
421 (dx > SystemParameters.MinimumHorizontalDragDistance ||
422 dy > SystemParameters.MinimumVerticalDragDistance))
423 {
424 _isDragging = true;
425
426 // \note DragMove 직전 캡처 해제 (드래그 씹힘 방지 핵심)
427 try
428 {
429 if (Mouse.Captured == this)
430 ReleaseMouseCapture();
431 else
432 Mouse.Capture(null);
433 }
434 catch { /* ignore */ }
435
436 try
437 {
438 var win = Window.GetWindow(this);
439 win?.DragMove();
440 }
441 catch
442 {
443 // DragMove 중 예외는 조용히 무시
444 }
445 }
446 }
447
480 private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
481 {
482 ReleaseMouseCapture();
483
484 if (_isDragging) return; // 드래그로 끝난 경우 클릭 아님
485
486 if (ClickCommand?.CanExecute(CommandParameter) == true)
487 {
488 try { ClickCommand.Execute(CommandParameter); } catch { /* 무시 */ }
489 }
490 }
491
492 }
493}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
static readonly DependencyProperty ClickCommandProperty
static readonly DependencyProperty IsDoubleClickToggleMaximizeProperty
void OnMouseMove(object sender, MouseEventArgs e)
static readonly DependencyProperty FallbackSourceProperty
static readonly DependencyProperty CornerRadiusProperty
void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
static readonly DependencyProperty IsWindowDragEnabledProperty
static readonly DependencyProperty SourceProperty
static readonly DependencyProperty CommandParameterProperty
static readonly DependencyProperty StretchProperty