Monday, December 1, 2014

Multiple ContentPresenters in content control's ControlTemplate

I have a custom control, that is inheriting ContentControl and have Content property (it is inheriting AppBarButton actually).


That control have several dependency properties (Icon, Content, Description + template and template selector properties for each), that I want to bind in the control's ControlTemplate.


ControlTemplate as follows (truncated for simplicity):



<ControlTemplate x:Key="IconButtonControlTemplate" TargetType="local:IconButton">
<Grid x:Name="RootGrid">
<StackPanel x:Name="ButtonStack"
Transitions="{TemplateBinding ContentTransitions}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Orientation="{TemplateBinding IconToContentOrientation}">

<ContentPresenter x:Name="IconPresenter"
Content="{TemplateBinding Icon}"
ContentTransitions="{TemplateBinding ContentTransitions}" />

<StackPanel x:Name="ContentStack" Orientation="{TemplateBinding ContentToDescriptionOrientation}">

<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
ContentTransitions="{TemplateBinding ContentTransitions}"
AutomationProperties.AccessibilityView="Raw"/>
<ContentPresenter x:Name="DescriptionPresenter"
Content="{TemplateBinding Description}"
ContentTemplate="{TemplateBinding DescriptionTemplate}"
ContentTemplateSelector="{TemplateBinding DescriptionTemplateSelector}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</StackPanel>

</StackPanel>

</Grid>
</ControlTemplate>

The problem is that when I set the Content property of my button, it affects all three ContentPresenters in the template. Every ContentPresenter shows the same content - what's set to the "Content" property.


If I do not set the Content property, the template works as expected. But when I set it, all ContentPresenters shows the same.


The dependency properties bound are normal dependency properties with "new PropertyMetadata(Nothing)". Bindings are OK, I've tried to use normal Binding with RelativeSource TemplatedParent and attached debugconvertor to it, the bindings works as expected, but the issue remains.


Have you face such a issue and does anybody knows how to tackle it?



No comments:

Post a Comment