Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I have a class
C#
public class Lane
{
      public int Number { get; set; }

      public Color Colour { get; set; }
}

Which is the model as part of a MVVM application.

I need to record the color of the lane and was wondering which class is the best to use so that the color can be displayed on the screen as both the actual color in a background to a control and the text name of the color eg Red.

Do I use color or am I better of using a brush?

How do I get the name of the color back again?

which color picker do people recommend I use. I've tried a few now and they return brushes (which makes me what to use that) instead of a color object.
Posted

1 solution

Your problem is that your property color does not change anything in graphics. Why do you think property mechanism was invented for? Mostly for side effect of the getter and event more importantly of the setter hidden behind the simple facade of assignment:

public class Lane
{
      //...
      public Color Colour {
          get { return GetColor(); }
          set { SetColor(value); } //change color of what's inside
      }
}


Now, Color vs Brush. If you need to provide the opportunity to paint something not in a single uniform color, you just have to use Brush, otherwise Color is better even if inside you use a Brush. Encapsulate and hide unwanted complexity from the outside user.

See http://en.wikipedia.org/wiki/Facade_pattern[^].

—SA
 
Share this answer
 
Comments
Michael Bookatz 14-Jun-11 13:58pm    
I don't understand what you mean in that it doesn't change anything in graphics? Why would it need to? It's only the model. The view via the viewmodel takes the color set in Lane to work out what is the background color.

how do you get the actual color name that is being used by the brush? I've looked in a few places and can't see any method that tells me.

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