开发者

Setting the UserControl property to child controls - WPF, Silverlight

开发者 https://www.devze.com 2023-01-10 00:39 出处:网络
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 t

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.

0

精彩评论

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

关注公众号