728x90
반응형
728x170
■ Thumb 엘리먼트 : DragStarted/DragDelta/DragCompleted 이벤트를 사용해 드래그하는 방법을 보여준다.
▶ MainWindow.xaml
<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="Thumb 엘리먼트 : DragStarted/DragDelta/DragCompleted 이벤트를 사용해 드래그 하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Canvas Margin="10">
<Canvas Name="canvas"
Width="200"
Height="200"
Background="Green">
<TextBox Name="textBlock" Canvas.Left="0" Canvas.Top="0"
Width="{Binding ElementName=canvas,Path=Width}"
Height="{Binding ElementName=canvas,Path=Height}"
BorderThickness="0"
Background="Green"
Foreground="Yellow"
Text="Size : 200, 200" />
<Thumb Name="thumb" Canvas.Left="180" Canvas.Top="180"
Background="Blue"
Width="20"
Height="20" />
</Canvas>
</Canvas>
</Window>
▶ MainWindow.xaml
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.thumb.DragStarted += thumb_DragStarted;
this.thumb.DragDelta += thumb_DragDelta;
this.thumb.DragCompleted += thumb_DragCompleted;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 썸 드래그 시작시 처리하기 - thumb_DragStarted(sender, e)
/// <summary>
/// 썸 드래그 시작시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void thumb_DragStarted(object sender, DragStartedEventArgs e)
{
this.thumb.Background = Brushes.Orange;
}
#endregion
#region 썸 드래그 델타 처리하기 - thumb_DragDelta(sender, e)
/// <summary>
/// 썸 드래그 델타 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
double width = this.canvas.Width + e.HorizontalChange;
double height = this.canvas.Height + e.VerticalChange;
if((width >= 0) && (height >= 0))
{
this.canvas.Width = width;
this.canvas.Height = height;
Canvas.SetLeft(this.thumb, Canvas.GetLeft(this.thumb) + e.HorizontalChange);
Canvas.SetTop (this.thumb, Canvas.GetTop (this.thumb) + e.VerticalChange );
this.textBlock.Text = $"크기 : {canvas.Width}, {canvas.Height}";
}
}
#endregion
#region 썸 드래그 완료시 처리하기 - thumb_DragCompleted(sender, e)
/// <summary>
/// 썸 드래그 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void thumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
this.thumb.Background = Brushes.Blue;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Button 엘리먼트 : Style 속성 사용하기 (0) | 2020.08.09 |
---|---|
[C#/WPF] Cursor 클래스 : 리소스 커서 로드하기 (0) | 2020.08.09 |
[C#/WPF] Mouse 클래스 : OverrideCursor 정적 속성을 사용해 커서 설정하기 (0) | 2020.08.09 |
[C#/WPF] Clipboard 클래스 : GetText 정적 메소드를 사용해 HTML 구하기 (0) | 2020.08.08 |
[C#/WPF] UIElement 클래스 : DragEnter/GiveFeedback/DragOver/Drop/DragLeave 이벤트를 사용해 드래그하기 (0) | 2020.08.08 |
[C#/WPF] Calendar 클래스 사용하기 (0) | 2020.08.08 |
[C#/WPF] Frame 클래스 : Navigate 메소드를 사용해 페이지 이동하기 (0) | 2020.08.08 |
[C#/WPF] Button 엘리먼트 : Background 속성 사용하기 (0) | 2020.08.08 |
[C#/WPF] BulletDecorator 엘리먼트 사용하기 (0) | 2020.08.08 |
[C#/WPF] DrawingVisual 클래스 사용하기 (0) | 2020.08.07 |
댓글을 달아 주세요