什么意思:
TemplatedParent是RelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型—即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