Click here to Skip to main content
15,891,951 members
Articles / Web Development / HTML

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.6K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Lobster.MVVM
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class MiniColorComponentCtrl : UserControl, INotifyPropertyChanged
    {
        public MiniColorComponentCtrl()
        {
            InitializeComponent();
        }


        public object InnerBackground
        {
            get { return (object)GetValue(InnerBackgroundProperty); }
            set { SetValue(InnerBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InnerBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InnerBackgroundProperty =
            DependencyProperty.Register("InnerBackground", typeof(object), typeof(MiniColorComponentCtrl), new UIPropertyMetadata());

        
        private double ConvertVPosToColorComponent(double pos)
        {
            if (Box.ActualHeight == 0) return 0;

            return ((1.0 - (double)pos / (double)Box.ActualHeight));
        }
        
        private double doubleValue;

        public double DoubleValue
        {
            get
            {
                return doubleValue;
            }
            private set
            {
                var d = value;
                d = Math.Max(Math.Min(d, 1), 0);
                if (doubleValue!= d)
                {
                    doubleValue = d; 
                    this.OnPropertyChanged("ByteValue");
                    this.OnPropertyChanged("DoubleValue");
                }
            }
        }

        public byte ByteValue
        {
            get
            {
                return (byte)(doubleValue * 255);
            }

            set
            {
                DoubleValue = (double) value / 255.0;
            }
        }
        

        #region ChangeNotifier
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        #endregion ChangeNotifier

        private void Box_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DoubleValue = this.ConvertVPosToColorComponent(e.GetPosition( (IInputElement) sender).Y);   
        }

        private void Box_MouseMove(object sender, MouseEventArgs e)
        {
            var i = (UIElement)sender;
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DoubleValue = this.ConvertVPosToColorComponent((int)e.GetPosition(i).Y);
                i.CaptureMouse();
            }
            else
            {
                i.ReleaseMouseCapture();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions