mike01010
mike01010

Reputation: 6038

Replacing ItemsControl ContentPresenter entirely with control being rendered

i'm using an ItemsControl to generate a list of controls based on my model. When looking at the visual tree, i noticed that each of the rendered control is wrapped in a ContentPresenter. The controls that are added are a 3rd party control and are designed to display a splitter between each control if they are siblings..this allows a user to size each control. For example the following will show a splitter between each of the controls at run time.

<StackPanel>
<3rdPartyControl />
<3rdPartyControl />
<3rdPartyControl />
</StackPanel>

When using an ItemsControl, each of the 3rdPartyControl are wrapped in a ContentPresenter, and thus no splitter is shown. I have tried various ways to try and solve this problem but unable to get this to work unless i write code behind to add each control rather than rely on Xaml. Does anyone know of a way to replace the contentpresenter completely (in my case with 3rdpartyControl)?

Thanks

Upvotes: 3

Views: 1647

Answers (2)

Clemens
Clemens

Reputation: 128013

In order to replace the ContentPresenter you could derive from ItemsControl and override the GetContainerForItemOverride method to create a specialized container control.

ListBox for example overrides this method to create a ListBoxItem as container for a new item object.

Upvotes: 6

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

That's true, every element that you add to ItemsControl is wrapped with ContentPresenter, you can find more details about it in the greate series of articles from dr.wpf ItemsControl a-z

One way I would suggest to try is to change ItemsControl to ListBox and make ListBox act like ItemsControl. In this case you can re-style/re-template ListBoxItem and replace ContentPresenter with your control. You would also need to stop selection support. Here is ListBox style that you need to change.

Upvotes: 0

Related Questions