Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Rectangle vs Border: Which is better for performance?

There are times when either a Border control or a Rectangle control will fulfill a need I have. An example would be when I'm implementing a ControlTemplate and I'm already applying a layering technique (i.e. stacking controls in a Grid), and I don't need different RadiusX or RadiusY applied to the corners.

However, when designing such ControlTemplates, sometimes I end up using several such layers of either Borders or Rectangles. As such, I realized I should probably be cognizant as to which control will have the least performance impact on the application. I see that their inheritance hierarchy diverges after FrameworkElement. And I also notice that the Border is a Decorator control (I've worked with Decorators but I'm not sure how they perform relative to other controls). Can someone shed light onto

1) How we might be able to draw some general conclusions about a control's performance impact based on a particular inheritance hierarchy?

2) How do Decorators, such as Border, perform relative to other controls?

3) Specifically regarding Border and Rectangle, which performs better?

like image 730
Jason Frank Avatar asked Feb 06 '13 21:02

Jason Frank


1 Answers

It's been my experience that WPF borders are a bit lighter-weight, but more importantly - they represent a somewhat different need, although they're often rendered the same on your screen. If I am composing something that includes rectangles as part of what it is, then the Rectangle is usually what is appropriate. If I am wanting to emphasize something on-screen, or indicate that an object has some different state - then I'll employ a border. I'll often bind the color, thickness, or visibility of that border to the state property of the model (or whatever applies in your case), but the essential difference is that the border is not a part of the object. It is a way of just making that object stand out, or be visible.

Or if it is some already-composed thing, like a TextBox, and I'm adding some color around it - that will usually be a border.

By keeping this distinction in mind, it helps your XAML tree make better sense for you, and gives you code that is easier to maintain later.

like image 181
JamesWHurst Avatar answered Nov 04 '22 00:11

JamesWHurst