728x90
반응형
728x170
▶ CustomSlider.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TestProject
{
/// <summary>
/// 커스텀 슬라이더
/// </summary>
public class CustomSlider : Slider
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 자동 이동 여부 속성 - AutoMoveProperty
/// <summary>
/// 자동 이동 여부 속성
/// </summary>
public static readonly DependencyProperty AutoMoveProperty = DependencyProperty.Register
(
"AutoMove",
typeof(bool),
typeof(CustomSlider),
new FrameworkPropertyMetadata(false, AutoMovePropertyChangedCallback)
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 디폴트 마우스 클릭시 이동 여부
/// </summary>
private bool defaultIsMoveToPointEnabled;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 자동 이동 여부 - AutoMove
/// <summary>
/// 자동 이동 여부
/// </summary>
public bool AutoMove
{
get
{
return (bool)GetValue(AutoMoveProperty);
}
set
{
SetValue(AutoMoveProperty, value);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
////////////////////////////////////////////////////////////////////// Event
#region 커스텀 슬라이더 PREVIEW 마우스 이동시 처리하기 - customSlider_PreviewMouseMove(sender, e)
/// <summary>
/// 커스텀 슬라이더 PREVIEW 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void customSlider_PreviewMouseMove(object sender, MouseEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
CustomSlider customSlider = sender as CustomSlider;
Point mousePoint = e.GetPosition(customSlider);
customSlider.Value = mousePoint.X / (customSlider.ActualWidth / customSlider.Maximum);
}
}
#endregion
////////////////////////////////////////////////////////////////////// Function
#region 자동 이동 여부 속성 변경시 콜백 처리하기 - AutoMovePropertyChangedCallback(d, e)
/// <summary>
/// 자동 이동 여부 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="e">이벤트 인자</param>
private static void AutoMovePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomSlider customSlider = d as CustomSlider;
if(customSlider != null)
{
if((bool)e.NewValue)
{
customSlider.defaultIsMoveToPointEnabled = customSlider.IsMoveToPointEnabled;
customSlider.IsMoveToPointEnabled = true;
customSlider.PreviewMouseMove += customSlider_PreviewMouseMove;
}
else
{
customSlider.IsMoveToPointEnabled = customSlider.defaultIsMoveToPointEnabled;
customSlider.PreviewMouseMove -= customSlider_PreviewMouseMove;
}
}
}
#endregion
}
}
728x90
▶ 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"
xmlns:local="clr-namespace:TestProject"
Width="800"
Height="600"
Title="Slider 클래스 : 커스텀 슬라이더 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<local:CustomSlider
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="300"
Height="25"
AutoMove="True"
Minimum="0"
Maximum="100"
Value="50" />
</Grid>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Shape 클래스 : 환형 진행바 사용하기 (0) | 2021.02.20 |
---|---|
[C#/WPF] 마우스를 사용해 엘리먼트 이동/회전/확대/축소하기 (0) | 2021.02.20 |
[C#/WPF] EventTrigger 클래스 : 코드로 이벤트 트리거 사용하기 (0) | 2021.02.20 |
[C#/WPF] Image 클래스 : 마우스를 사용해 이미지 회전시키기 (0) | 2021.02.20 |
[C#/WPF] ComponentDispatcher 클래스 : ThreadFilterMessage 정적 이벤트를 사용해 프로세스 간 메시지 송수신하기 (0) | 2021.02.20 |
[C#/WPF] Slider 클래스 : 커스텀 슬라이더 사용하기 (0) | 2021.02.20 |
[C#/WPF] Window 클래스 : 반투명 윈도우 사용하기 (0) | 2021.02.19 |
[C#/WPF] MediaElement 클래스 : 움직이는 GIF 파일 재생하기 (0) | 2021.02.19 |
[C#/WPF] 워드 클라우드(Word Cloud) 이미지 만들기 (0) | 2021.02.19 |
[C#/WPF] 로딩 패널 표시하기 (0) | 2021.02.18 |
[C#/WPF] BitmapImage 클래스 : 바이트 배열에서 비트맵 이미지 구하기 (0) | 2021.02.18 |
댓글을 달아 주세요