Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have this XAML
XML
<Border CornerRadius="10" removed="{Binding SelColor, Mode=OneWay}" BorderBrush="Black" BorderThickness="1" Margin="7">
            <Rectangle MinHeight="20" MinWidth="180" Fill="Transparent" />
        </Border>
        <Expander Header="ChooseColor" Expanded="Expander_Expanded" Name="Exp" Collapsed="Exp_Collapsed">
            <StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="R" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource RedSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.R, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="G" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource GreenSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.G, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="B" Foreground="{Binding Foreground}"/>
                    <Slider removed="{DynamicResource BlueSliderGrad}" Minimum="0" Maximum="255" MinWidth="180" Value="{Binding SelColor.Color.B, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ValueChanged="Slider_ValueChanged"/>
                </StackPanel>
            </StackPanel>
        </Expander>
    </StackPanel>


and this C# code:

C#
public SolidColorBrush SelColor
        {
            get { return (SolidColorBrush)GetValue(SelColorProperty); }
            set
            {
                SetValue(SelColorProperty, value);
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SelColor"));
            }
        }

        // Using a DependencyProperty as the backing store for SelColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelColorProperty =
            DependencyProperty.Register("SelColor", typeof(SolidColorBrush), typeof(ColorChooser), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 204, 204, 204))));

        public ColorChooser()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            //this.Exp.IsExpanded = true;
            //this.Height += this.Exp.ActualHeight;
        }

        private void Exp_Collapsed(object sender, RoutedEventArgs e)
        {
            //this.Exp.IsExpanded = true;
            //this.Height -= this.Exp.ActualHeight;
            //this.Exp.IsExpanded = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

        }


I noticed that, as the slider changes, even the SelColor changes, but that changement is not reflected by the background of the border... Why?

Thanks!

Jymmy097
Posted

1 solution

The Color of a SolidColorBrush is a struct, not a class, so you can't bind to it the same way as a class, because when it is referenced by the binding, it gets a copy of the SelColor.Color from the SolidColorBrush, not the same actual instance of the Color.
 
Share this answer
 
Comments
LLLLGGGG 14-Mar-14 8:58am    
Thanks!
I've solved in this way:

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
this.SelColor = new SolidColorBrush(Color.FromArgb(255, (byte)this.SRed.Value, (byte)this.SGreen.Value, (byte)this.SBlue.Value));
}

Aren't there any better solutions?

Thanks!

-Jymmy097
Matt T Heffron 14-Mar-14 12:49pm    
I was thinking this might be the simplest way to handle it.
If you really wanted to do it strictly via bindings, then create 3 public properties on ColorChooser for the R,G,B values.
Bind to those properties, and in their setters, change the Color property of the SelColor, and (probably) raise the PropertyChangedEvent on "SelColor".
LLLLGGGG 15-Mar-14 13:33pm    
Thanks a lot!!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900