locked
Changing Color of a Brush doesn't work in Blend RRS feed

  • Question

  • Hi

    I'm working on solution to use different shades of color for Brushes. Therfor I implemented an atteched property for the Brush class called Brightness. That allows me to control the B channel in the HSB color space. That works fine in VS2010 and rubtime. However, Blend ignores the Color change made by propertry. Here the code:

     

     

    Any ideas??
    Please remember to mark the replies as answers if they answered your question!

     

            public static readonly DependencyProperty BrightnessProperty = DependencyProperty.RegisterAttached("Brightness", typeof(Double), typeof(SolidColorBrush), new PropertyMetadata(1.0, new PropertyChangedCallback(OnBrightnessChanged)));
    
            public static void SetBrightness(DependencyObject obj, double shade)
            {
                obj.SetValue(BrightnessProperty, shade);
            }
    
            public static double GetBrightness(DependencyObject obj)
            {
                return (double)obj.GetValue(BrightnessProperty);
            }
    
            private static void OnBrightnessChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                 ((Brush)o).ChangeBrightness((Double)e.NewValue);
            }
    


     

    Thursday, December 15, 2011 2:23 AM

All replies

  • I'm not understanding "Blend ignores the Color change made by property." Is the dependency property not showing up? Are you calling the property, but it has no effect? Does the XAML change, but the change is not reflected in the design surface? More information would help us figure out what's happening.
    Friday, December 16, 2011 4:30 PM
  • The code in the DependencyProperty BrightnessProperty  changes the original Color of the Brush. That works fine in VS and at runtime. However, Blend doesn't show the changed Color.

    I already found the reason and a workaround. The problem how Blend is parsing the properties in XAML. VS and Silverlight runtime parsing the properties of XAML tag in the order in that they appear. E.g. this works:

    <SolidColorBrush x:Key="LightBlue" Color="Blue" ext:BrushExt.Brightness="0.5" />

    this doesn't work

    <SolidColorBrush x:Key="LightBlue" ext:BrushExt.Brightness="0.5" Color="Blue"

    />

     However, it looks like Blend parses my AttachedProperty always first and then the Color. I couldn't find a way to recognize the change of the Color and reapply my brightness settings. So I ended up with quirks mode in my DependencyProperty for Blend.  

    public static readonly DependencyProperty BrightnessProperty = DependencyProperty.RegisterAttached("Brightness", typeof(Double), typeof(SolidColorBrush), new PropertyMetadata(0.0, new PropertyChangedCallback(OnBrightnessChanged)));
    
            public static void SetBrightness(DependencyObject obj, double shade)
            {
                obj.SetValue(BrightnessProperty, shade);
            }
    
            public static double GetBrightness(DependencyObject obj)
            {
                return (double)obj.GetValue(BrightnessProperty);
            }
    
            private static void OnBrightnessChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                if (DesignerProperties.IsInDesignTool)
                {
                    //quirks mode to make it work in Blend
                    DispatcherTimer t = new DispatcherTimer();
                    t.Interval = TimeSpan.FromSeconds(0.1);
                    t.Tick += (s, args) =>
                        {
                            t.Stop();
                            ((Brush)o).ChangeBrightness((Double)e.NewValue);
                        };
                    t.Start();
                }
                else
                {
                    ((Brush)o).ChangeBrightness((Double)e.NewValue);
                }
            }
    

     

    The design surface in Blend shows the colors properly now. The only problem is that the Brushes Resource window don't get updated if I change the color of Brush til I reload the project. When I was searching for a solution I found more problems with Blend and VS not showing colors properly. E.g. markup extension that returns a Brush don't work in VS/Blend but at runtime. See here: http://forums.silverlight.net/t/244547.aspx/1?simple+markup+extension+not+working+at+DesignTime


    Please remember to mark the replies as answers if they answered your question!

    Friday, December 16, 2011 6:19 PM