0

I'm looking to create a bunch of rectanges that are going to share some properties and some other properties will be different. This is all done in codebehind, and clearly it is very possible to do this without breaking a sweat by copy and paste skills, but in the spirit of making my code more elegant; is it possible to have a sample rectangle like so

Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)};

and model everyother rectangle after that with diefferent height and width attributes?

UPDATE Thanks for the answers, I am actually looking for more of a CSS/style thing...

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Sevki
  • 3,592
  • 4
  • 30
  • 53

2 Answers2

0

you could have Class that represents your Rectangle parameters and use DataTemplate to convert your class into Rectangle in your XAML

and your class will have default Strock and Margin and you can override height and width

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
0

You could wrap it inside a method, like this (assuming strokebrush is some sort of local field)

private static Rectangle RectangleBuilder(int height, int width)
{
   Rectangle sampleRect = new Rectangle()
   {
       Stroke = strokebrush,
       Margin = new Thickness(5),
       Height = height,
       Width = width
   };
   return sampleRect;
}
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74