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 hav开发者_运维问答e 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.
Take a look at RelativeSourceMode.FindAncestor
<TextBlock
Text="{Binding RelativeSource={RelativeSource RelativeSourceMode=FindAncestor, AncestorType={x:Type Controls:Expander}}}, Path=Header"/>
Just do this
<DataTemplate x:Key="MyExpanderHeaderTemplate">
<TextBlock Text="{Binding}"/>
</DataTemplate>
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!
have you tried:
<DataTemplate x:Key="MyExpanderHeaderTemplate">
<TextBlock Text="{Binding .}">
// some triggering stuff
</TextBlock>
</DataTemplate>
精彩评论