0

Consider a custom control, with a ControlTemplate defined via Style in a ResourceDictionary:

<Style TargetType="{x:Type local:MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyControl}">
                ....

Some where in that template, I have a ContentControl which I want to set its content to the TemplatedParent itself.

If I bind it to a property on the TemplatedParent it works great:

Content="{TemplateBinding LayoutMode}"

But is there anyway to bind it to the TemplatedParent Itself? And if not are there any workarounds?

Omri Btian
  • 6,499
  • 4
  • 39
  • 65

1 Answers1

1

Maybe using

BoundProperty="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}

?

nb: if you're intending to bind a "Content" property to fill the "content" (i.e. visual child) of a control, that wont work...

Olivier
  • 5,578
  • 2
  • 31
  • 46
  • That is what I'm trying, And I guess this is why it doesn't work because I get `Logical tree depth exceeded while traversing the tree. This could indicate a cycle in the tree.` – Omri Btian Dec 03 '13 at 10:24
  • Even if it worked, your templated parent already is in the tree... you cannot insert it elsewhere. What you're looking for is ContentPresenter, I guess... – Olivier Dec 03 '13 at 10:26
  • `ContentPresenter` worked! Thanks, So what is the difference between `ContentPresenter` and `ContentControl`? – Omri Btian Dec 03 '13 at 10:30
  • In a nutshell, ContentPresenter is made to display elements determined runtime. It may be considered as a **placeholder**. see [this post](http://stackoverflow.com/questions/1287995/whats-the-difference-between-contentcontrol-and-contentpresenter) – Olivier Dec 03 '13 at 10:44
  • 1
    to know more about it, I sugget you to decompile ContentControl.OnContentChanged and ContentPresenter.OnContentChanged. They're quite interresting: The first one calls AddLogicalChild, while the second doesnt. (no logical cycle using the ContentPresenter, thus) – Olivier Dec 03 '13 at 10:50