steveh
Topic Author
Posts: 38
Joined: 07 Oct 2019, 12:50

Template hierarchies and clipping

05 Mar 2021, 21:49

Hi guys, I have a question about how best to design the architecture for a bunch of my templates, first an example:

https://www.noesisengine.com/xamltoy/f8 ... 1fdd4a11fb

So in our game we have something similar to this setup. We have a tool kit where we have a generic custom ControlTemplate for a ListBoxItem. This ControlTemplate contains general functionality for all sorts of visual states that an item might appear in. The template is really generic and is reused with lots of different DataTemplates in a multitude of different use cases. We create a clipping region on the control template as we have elements that need to be clipped to the size of the content. The DataTemplate dictates the size of the control template, but the control template has an animation which plays over the top of the DataTemplate and has to be clipped by the border. So, what we have:
  • The ControlTemplate defines the border, background and clipping region
  • The DataTemplate defines the size and data that appears within the ListBoxItem
  • The ControlTemplate is fairly complicated, so I don't want to implement a custom version of this for this specific use case if I can avoid it, otherwise we have to support the controltemplate and all the visual states twice.
  • The DataTemplates are relatively simple and contain just the data that should appear on the buttons.
This all worked fine until today when the artist gave me a pre-vis of them using a custom button where some part of the DataTemplate overlaps the border and does not want to be clipped.

I've tried to think of a way around this and I can't think of an elegant solution. As I said, our control template is used in a large part of our game across a variety of different menus and for lots of different use cases so has lots of visual states and storyboards so I don't want to support a custom version of this. The control template also needs to be the one to define the clipping region as it has elements that needs to be clipped to the region of the data template.

Do you have any suggestions? These are the things I've thought of so far:
  1. Clone the control template and just add in the elements which overlay the border to the ControlTemplate. This would require us to maintain 2 ControlTemplates.
  2. Create a custom ControlTemplate that has 2 content templates and 2 content dependency properties. Our ControlTemplate would then have 2 content pesenters in, one for the normal content and one for the secondary content. This would mean creating a custom control that you generally don't advise doing.
  3. Move the clipping region into the DataTemplate. This would mean that we'd have to have the same clipping code in all our DataTemplates (we have lots), and also the ControlTemplate would not clip any elements.
So far the best solution I can think of is to create a custom control to allow us to specify 2 data templates (an inner and an outer data template), but I know you usually advise against doing that. Any thoughts before I start going down that route?

Cheers,

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2323
Joined: 22 Dec 2011, 19:20

Re: Template hierarchies and clipping

08 Mar 2021, 10:31

Hi Steve, will the secondary template always be the same or it depends on the item data type? Because I'm thinking on just including 2 ContentPresenters in the ListBoxItem template, and apply a specific DataTemplate for the second one. And although secondary template depens on the data type you can use a DataTemplateSelector for that purpose.



Could that work for you?
 
steveh
Topic Author
Posts: 38
Joined: 07 Oct 2019, 12:50

Re: Template hierarchies and clipping

08 Mar 2021, 11:17

Hi Sergio, cheers for the suggestion. A single DataTemplate would not work as the overlay depends on the datatype, but as you suggested a DataTemplateSelector would work.

Thank you very much for the suggestion, I think that is a very good solution! I'll create an "Overlay data template selector" which returns the correct data template (or none) depending on some condition.

Much appreciated for the advice!

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2323
Joined: 22 Dec 2011, 19:20

Re: Template hierarchies and clipping

08 Mar 2021, 11:31

Glad to be helpful :)

Who is online

Users browsing this forum: No registered users and 1 guest