I have an implementation question. I am trying to have a rectangle change color based on the position of four sliders. (Alpha, Red, Green, Blue) I am attempting to use a dependency property to combine all four values, convert them to a brush, and apply the brush to a rectangle. First question is, is it possible to do this with one property? If not, do I need a property for each slider and a property that is accessed each time one of the sliders change?

you could have a color property. when you adjust a slider just update that component of the color property

private void redTB_Scroll(object sender, EventArgs e)
        {
            m_red = redTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void GreenTB_Scroll(object sender, EventArgs e)
        {
            m_green = GreenTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void BlueTB_Scroll(object sender, EventArgs e)
        {
            m_blue = BlueTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            m_alpha = Convert.ToInt32(alphaUD.Value);
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

this is a cut from a different project i made but it shows the principle

Thanks for your quick response. Sadly, I am not allowed to use events in that sense. I have to use something of this nature. I apologize for not being more clear. I have been reading Pro WPF 2008 and Pro C# 2010 in order to find out how to bind the value change to a canvas, but there is not much that I understand. I've been able to implement the binding using a string, but not with colors.

public Color Color {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Color.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", 
            typeof(Color), 
            typeof(ColorSelector), 
            new UIPropertyMetadata(Colors.Red));

you could have a color property. when you adjust a slider just update that component of the color property

private void redTB_Scroll(object sender, EventArgs e)
        {
            m_red = redTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void GreenTB_Scroll(object sender, EventArgs e)
        {
            m_green = GreenTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void BlueTB_Scroll(object sender, EventArgs e)
        {
            m_blue = BlueTB.Value;
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            m_alpha = Convert.ToInt32(alphaUD.Value);
            colorBox.BackColor = Color.FromArgb(m_alpha, m_red, m_green, m_blue);
        }

this is a cut from a different project i made but it shows the principle

I understand now. I can use the events to influence the property. Thanks. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.