views:

47

answers:

2

I have a Usercontrol with a Button 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?

+1  A: 

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.

Kelly
+1  A: 

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

benPearce