tags:

views:

28

answers:

1

I'm trying to create a ItemsControl that has a separator between items, for example a control to create a navigation bread crumb. I want the control to be completely generic.

My original method was to create extend ItemsControl, add a SeparatorTemplate property, and then have the class add separators to the ItemsHost of the ItemsControl. The problem with this approach is that if you add extra items to the container panel, the ItemGenerator gets confused and the items are out of order and don't get removed correctly.

So my second plan was to create a completely new control that would emulate an ItemsControl, but the problem I'm running into is that I can't find a way to instantiate an ItemsPanelTemplate. I would like to provide an ItemsPanel property just like ItemsControl, but I can't then create a panel from that template.

Can anyone think of a way to either instantiate an ItemsPanelTemplate or way to add controls to an ItemsControl's panel without breaking the ItemGenerator?

A: 

Well I haven't tried it myself but I would have thought you would override the GetContainerForItemOverride to acheive this.

You could create a new BreadCrumbItem control which is a templated ContentControl that has in its default template the typical ContentPresenter and whatever you want to use by default as separator all in a Grid or StackPanel.

The GetContainerForItemOverride generates a new instance of this BreadCrumbItem sets its ContentTemplate to the ItemTemplate property from your ItemsControl derivative (the BreadCrumb control?).

Your BreadCrumb control would also expose BreadCrumbItemStyle property that you assign to the BreadCrumbItem you create during GetContainerForItemOverride.

For completeness you may need to also implement the other *Container*Override methods in your BreadCrumb control.

AnthonyWJones
I was hoping to avoid having to include the separator in the ItemContainer. If I include it in the default template then I need manage what the last item is so I can make the separator not visible on the last item. The other problem with including it in the ItemContainer is that it won't follow the layout of the ItemsPanel. I want to be able to supply any ItemsPanel and have it correctly layout just as if it was another item.
Stephan
@Stephan: Good points. Including a "LastItem" visual state may help with the extra separator "management" is unavoidable regardless of the solution. As to the orientation thats a choice the designer replacing your default panel is making they'll just have to replace the `BreadCrumbItem` template via the `BreadCrumbItemStyle` property as well. Personally I think this a reasonable place to be. The control does an acceptable job out of the box and can be styled beyond recognition if so desired by the consumer.
AnthonyWJones
@Anthony Well I decided to overload ItemsSource and then insert separators into the list and push that into the actual ItemsSource. I know it may not be the safest way since someone can access `base.ItemsSource` themselves, but it gave me what I'm looking for. Thanks for your help.
Stephan