user510527
user510527

Reputation:

How to bind property of parent element

i have a quick Binding Question about Silverlight. I have some Expander and want to overwrite their Header Templates

<Controls:Expander Header="MyHeaderTitle"
                   HeaderTemplate="{StaticResource MyExpanderHeaderTemplate}">
   //Content
</Controls:Expander>
<Controls:Expander Header="MyNextHeaderTitle"
                       HeaderTemplate="{StaticResource MyExpanderHeaderTemplate}">
    //Content
</Controls:Expander>

In the header template i have an textbox and want to bind the text to the Header of the expander.

<DataTemplate x:Key="MyExpanderHeaderTemplate">
    <TextBlock Text="{Binding Path=Header}">
       // some triggering stuff
    </TextBlock>
</DataTemplate>

I tried some stuff with RelativeSource (Self and TemplatedParent) but nothing seems to work. Some Ideas would be great, thx.

Upvotes: 2

Views: 1000

Answers (4)

user510527
user510527

Reputation:

Thanks for the quick answers. FindAncestor doesn't seem to work under Silverlight the Way it does in WPF (can't resolve AncestorType..) But {Binding} or {Binding .} do the trick!

Upvotes: 1

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84674

Just do this

<DataTemplate x:Key="MyExpanderHeaderTemplate"> 
    <TextBlock Text="{Binding}"/>
</DataTemplate> 

Upvotes: 1

TerenceJackson
TerenceJackson

Reputation: 1786

have you tried:

<DataTemplate x:Key="MyExpanderHeaderTemplate">
    <TextBlock Text="{Binding .}">
       // some triggering stuff
    </TextBlock>
</DataTemplate>

Upvotes: 0

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9265

Take a look at RelativeSourceMode.FindAncestor

<TextBlock 
  Text="{Binding RelativeSource={RelativeSource RelativeSourceMode=FindAncestor, AncestorType={x:Type Controls:Expander}}}, Path=Header"/>

Upvotes: 1

Related Questions