첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요