Пример #1
0
        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _gradientCanvas      = Template.FindName("PART_GradientCanvas", this) as Canvas;
            _gradientSelector    = Template.FindName("PART_GradientSelector", this) as FrameworkElement;
            _colorSpectrumSlider = Template.FindName("PART_ColorSpectrumSlider", this) as ColorSpectrumSlider;
            _tbA         = Template.FindName("PART_TextBoxA", this) as TextBox;
            _tbR         = Template.FindName("PART_TextBoxR", this) as TextBox;
            _tbG         = Template.FindName("PART_TextBoxG", this) as TextBox;
            _tbB         = Template.FindName("PART_TextBoxB", this) as TextBox;
            _tbHexString = Template.FindName("PART_TextBoxHexString", this) as TextBox;

            if (_gradientCanvas != null)
            {
                _gradientCanvas.PreviewMouseLeftButtonDown += (o, e) =>
                {
                    var p = e.GetPosition(_gradientCanvas);
                    UpdateColorByPosition(p);
                    _gradientCanvas.CaptureMouse();
                    e.Handled = true;
                };
                _gradientCanvas.MouseMove += (o, e) =>
                {
                    if (_gradientCanvas.IsMouseCaptured && e.LeftButton == MouseButtonState.Pressed)
                    {
                        var position = e.GetPosition(_gradientCanvas);
                        UpdateColorByPosition(position);
                    }
                };
                _gradientCanvas.MouseLeftButtonUp += (o, e) =>
                {
                    _gradientCanvas.ReleaseMouseCapture();
                };
                _gradientCanvas.SizeChanged += (o, e) =>
                {
                    var hslColor = _lastPreciseColor ?? HslColor.FromArgb(Color);
                    hslColor.H = _colorSpectrumSlider.Value;
                    UpdateColor(hslColor.ToArgb(), hslColor);
                };
            }
            if (_colorSpectrumSlider != null)
            {
                _colorSpectrumSlider.ValueChanged += (o, e) =>
                {
                    var hslColor = _lastPreciseColor ?? HslColor.FromArgb(Color);
                    hslColor.H = _colorSpectrumSlider.Value;
                    UpdateColor(hslColor.ToArgb(), hslColor);
                };
            }

            UpdateColor(Color, null);
        }
Пример #2
0
        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _gradientCanvas = Template.FindName("PART_GradientCanvas", this) as Canvas;
            _gradientSelector = Template.FindName("PART_GradientSelector", this) as FrameworkElement;
            _colorSpectrumSlider = Template.FindName("PART_ColorSpectrumSlider", this) as ColorSpectrumSlider;
            _tbA = Template.FindName("PART_TextBoxA", this) as TextBox;
            _tbR = Template.FindName("PART_TextBoxR", this) as TextBox;
            _tbG = Template.FindName("PART_TextBoxG", this) as TextBox;
            _tbB = Template.FindName("PART_TextBoxB", this) as TextBox;
            _tbHexString = Template.FindName("PART_TextBoxHexString", this) as TextBox;

            if (_gradientCanvas != null)
            {
                _gradientCanvas.PreviewMouseLeftButtonDown += (o, e) =>
                {
                    var p = e.GetPosition(_gradientCanvas);
                    UpdateColorByPosition(p);
                    _gradientCanvas.CaptureMouse();
                    e.Handled = true;
                };
                _gradientCanvas.MouseMove += (o, e) =>
                {
                    if (_gradientCanvas.IsMouseCaptured && e.LeftButton == MouseButtonState.Pressed)
                    {
                        var position = e.GetPosition(_gradientCanvas);
                        UpdateColorByPosition(position);
                    }
                };
                _gradientCanvas.MouseLeftButtonUp += (o, e) =>
                {
                    _gradientCanvas.ReleaseMouseCapture();
                };
                _gradientCanvas.SizeChanged += (o, e) =>
                {
                    var hslColor = _lastPreciseColor ?? HslColor.FromArgb(Color);
                    hslColor.H = _colorSpectrumSlider.Value;
                    UpdateColor(hslColor.ToArgb(), hslColor);
                };
            }
            if (_colorSpectrumSlider != null)
            {
                _colorSpectrumSlider.ValueChanged += (o, e) =>
                {
                    var hslColor = _lastPreciseColor ?? HslColor.FromArgb(Color);
                    hslColor.H = _colorSpectrumSlider.Value;
                    UpdateColor(hslColor.ToArgb(), hslColor);
                };
            }

            UpdateColor(Color, null);
        }