9

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?

Jason Frank
  • 3,842
  • 31
  • 35
  • To be honest, I never had performance issues with borders or rectangles. – Mare Infinitus Feb 06 '13 at 21:40
  • I don't think using a border or a rectangle affect your application performance that much, you should worry about effects (like Shadow) and animations instead, the way they can affect your application performance is considerable. In that cases when it does not affect performance considerably, I prefer to increase code readability, using border when a border is needed and using a rectangle when a rectangle is needed. – Saman Hakimzadeh Abyaneh Dec 11 '18 at 09:32
  • this has to be a go to for any graphic performance questions in WPF: https://stackoverflow.com/a/33532790/347484 – Boppity Bop Apr 12 '21 at 12:35

1 Answers1

2

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.

JamesWHurst
  • 493
  • 8
  • 14