I have a Usercontrol with a Butt开发者_开发问答on and TextBlock in it. I would like to set the Background of both Button and TextBlock with the Background of the Usercontrol.(ie, I am trying to Bind to the usercontrol's Background property).
Please let me know how can I do this in XAML.
Is there any difference for this in Silverlight and WPF?
You can bind to a Ancestor control such as:
<Button Background="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=Background}"/>
The same will work for the TextBlock, but its Background is already transparent
According to this Silverlight does not support FindAncestor
Use TemplateBinding:
<Border Background="{TemplateBinding Background}">
<TextBlock Background="{TemplateBinding Background}" Text="something"/>
</Border>
Just set both the top level control in your usercontrol (a border in my case) and the TextBlock to use the TemplateBinding Background.
精彩评论