jayarjo
jayarjo

Reputation: 16726

Is it possible to define visual style from code rather then from xaml?

I would like to create (extend core button class probably) basic button programmatically on request and add it to a main canvas when ready. Is it possible to set a styling from the code (it is going to be width/height and transparent background, nothing else), rather then having visual styles in xaml file?

Upvotes: 1

Views: 82

Answers (1)

Emond
Emond

Reputation: 50672

A Style is just another class.

Style buttonStyle = new Style(typeof(Button));
buttonStyle.Setters.Add(new Setter(Button.WidthProperty, 100));
buttonStyle.Setters.Add(new Setter(Button.HeightProperty, 75));
buttonStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidBrush(...)));

myButton.Style = buttonStyle;

Upvotes: 4

Related Questions