绑定-TemplatedParent

什么意思

TemplatedParentRelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型TargetType;如果该绑定是在模板中,那么它的作为范围也只限于该模板—It is applicable if the Binding is within a template

 

如何使用

    <Style TargetType="{x:Type local:TemplatedParent}">

        <Setter Property="Background" Value="Green"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type local:TemplatedParent}">

                    <Grid>

                        <Ellipse>

                            <Ellipse.Fill>

                                <SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>

                            </Ellipse.Fill>

                        </Ellipse>

                    </Grid>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

在上例中是这样使用TemplatedParent的:

<SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>

 

这样绑定的源元素就指向local:TemplatedParent这个目标类型了,所以当你修改目标类型的背景颜色时,Ellipse也将跟随它变化。

 

 

参考资料:

WPF揭秘》

MSDN

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值