tags:

views:

120

answers:

6

Is there an existing .net class (or available one) that already encapsulates all/most of the options available thru css?

ie: MyStyleClass msc = new MyStyleClass(); msc.font-family = "arial";

Im looking for an existing class that exposes these properties w/o style("font-family") = "arial";

I didnt see anything while searching through existing .net framework, but i may not be looking for the right thing.

Why? I need this functionality and plan to create the actual .css from the properties set in the class and i dont want to re-invent the wheel.

:-)


update

Thank you for your answers so far, but my goal was not to have to create this class - i was hoping there was either a built-in one or 3rd party one (free) with properties, enums, etc. already exposed so i could just declare the object and start using it. I will create it if needed, just didnt want to re-invent the wheel :-)

A: 

One possibility is to use a Dictionary, i.e.:

Dictionary<string, string> msc = new Dictionary<string, string>();
msc["font-family"] = "arial";
ChrisW
Thank you but Im hoping not to have to create my own and i want it strong types, so i could do something like:msc.FontFamily = "arial";however if i can find nothing, i will create one and post
schmoopy
better yet, when i type msc.FontFamily, i would like an enum of existing font names to come up :-)
schmoopy
A: 

You will have to do some kind of mapping in order to achieve that.

There exists a class (System.Web.UI.WebControls.Style) which exposes basic properties including Font (which in-turn has Names as its property that can accept font names)

EDIT: See if CSSStyleCollection.Add method helps as well.

shahkalpesh
A: 

Is there an existing .net class (or available one) that already encapsulates all/most of the options available thru css?

No would be the answer, in the context of your question "all/most". I'm assuming you want something that intelligently knows what kind HTML tag this special class is attached to and offers up all the relative CSS selectors/rules with gets/sets and writes them appropriately? That doesn't exist, however, not a bad idea for a 3rd party product.

If you are looking for something simpler, that doesn't intelligently know it's associated html but yet has all/most CSS selectors, that doesn't exist either and you will have to roll your own with CSSStyleCollection.Add like shahkalpesh suggested.

Marc
A: 

Yes, you can use the Style property of most display objects, and the enums of HtmlTextWriterStyle are your corresponding css properties:

HyperLink hl = new HyperLink() { Text = "StackOverFlow", NavigateUrl = "http://stackoverflow.com" };
hl.Style.Add(HtmlTextWriterStyle.Color, "red");
hl.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");

That makes a hyperlink and adds the in-line style: "color:red;font-weight:bold;"

naspinski
+2  A: 

This design violates the whole reason for CSS - seperating code and structure from presentation. Designing a CSS producing .net class defeats the whole reason for CSS, which is to allow graphic designers to redesign a website without knowledge of the underlying code.

The correct solution is to output a class name on elements you want to give a specific style and then put the style rules in a pure CSS file. It may not be the answer that YOU want but it is the correct answer nonetheless.

EDIT: Mistunderstood the question (see comment). For client side parsing try wrapping a C library like libCSS.

SpliFF
Imagine I'm writing a browser: I therefore need to parse a CSS file (in order to apply the styles which it specifies to the DOM nodes which I'm drawing). In that case, what would be a good object model for parsed CSS?
ChrisW
I misunderstood your question then. In that case I'd suggest creating a binding to an existing C library like libcss (not to be confused with the DVD decryptor) or see if there are existing ones for WebKit or even (blergh), IE. http://www.netsurf-browser.org/projects/libcss/
SpliFF
I'm not the OP. You probably didn't misunderstand the original question: I asked a different, supplementary question. Thanks for your answer by the way.
ChrisW
The purpose was to generate a .css file that would then be modifed by designers, but also modified by non designers using a UI. :-)
schmoopy
A: 

I've written and released a CSS parser, at http://www.modeltext.com/css/

The object model of its lowest-level API is a one-to-one representation of a stylesheet, based on the CSS grammar (see the ModelText.ModelCssInterfaces.Parsed namespace).

My component only support reading, not writing; but it's a good example of the API/object model which you can implement, from which you can "create the actual .css from the properties".

There's also a W3C DOM API for CSS.

ChrisW