开发者

TemplatedParent - A clarification

Here's the Tree:

  • MyControl : BOUND(IContentMenuItem)
    • ControlTemplate
      • ContentPresenter : BOUND(Header, HeaderTemplate, HeaderTemplateSelector)
        • DataTemplate (For Header)
          • Label

I Want to bind The Label @ the end of that tree to a property on the DataContext of MyControl

I can use Fi开发者_JAVA百科ndAncestor to get the MyControl (Obviously) but how do I bind to it's DataContext (of type IContentMenuItem)


Now for the Templated Parent Question

Will Binding to the TemplatedParent inside the DataTemplate in effect bind to The ContentPresenter, The ControlTemplate, or The MyControl ?


I think the answer to the TemplatedParent question is the ContentPresenter -- it is whatever the template is being applied to.

If you can get MyControl using FindAncestor, then you can bind to properties of the DataContext like this:

<Label Content="{Binding DataContext.MyProperty, RelativeSource=…}" />

You can also just set an x:Name for MyControl and then the binding looks like:

<Label Content="{Binding DataContext.MyProperty, ElementName=myControl}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜