通过RelativeSource绑定到Parent属性

时间:2012-04-17 21:58:55

标签: silverlight binding

我无法弄清楚什么是错的。 我希望将DropShadowEffect的颜色绑定到TextBox Foreground颜色。当我使用ElementName绑定属性进行绑定时,它工作正常,但我需要使用RelativeSource(在VisualThree中查找第一个TextBox对象)。

下面是我的代码(它是Silverlight 5):

<Grid x:Name="LayoutRoot" Background="White">

<TextBox x:Name="TextBox1" Foreground="Blue" MinWidth="100" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBox.Effect>
        <DropShadowEffect x:Name="ShadowEffect" BlurRadius="60" Direction="345" ShadowDepth="50" Color="{Binding Path=Foreground.Color, RelativeSource={RelativeSource AncestorType=TextBox}}"></DropShadowEffect>
    </TextBox.Effect>
</TextBox>

提前感谢所有人,

Saulo

1 个答案:

答案 0 :(得分:0)

你应该使用{RelativeSource self},因为你的效果属性依赖于TextBox(“TextBox1”),而不是TextBox父级。

<TextBox x:Name="TextBox1" Foreground="Blue" MinWidth="100" HorizontalAlignment="Center" VerticalAlignment="Center"> 
<TextBox.Effect> 
    <DropShadowEffect x:Name="ShadowEffect" BlurRadius="60" Direction="345" ShadowDepth="50" Color="{Binding RelativeSource={RelativeSource self}, Path=Foreground.Color}"></DropShadowEffect> 
</TextBox.Effect> 

相关问题