■ Binding 태그 확장의 RelativeSource 속성을 사용하는 방법을 보여준다.
▶ TextBlock 자신이 바인딩 소스인 경우 (C#)
1 2 3 |
<TextBlock Text="{Binding RelativeSource={RelativeSource self}, Path=FontFamily}" /> |
▶ TextBlock 기준 1단계 위의 StackPanel이 바인딩 소스인 경우 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
<StackPanel TextBlock.FontSize="12" > <StackPanel HorizontalAlignment="Center" Orientation="Horizontal"> <TextBlock Text="This TextBlock is inside a StackPanel with " /> <TextBlock Text= "{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Orientation}" /> <TextBlock Text=" orientation" /> </StackPanel> </StackPanel> |
▶ TextBlock 기준 2단계 위의 StackPanel이 바인딩 소스인 경우 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
<StackPanel TextBlock.FontSize="12" > <StackPanel HorizontalAlignment="Center" Orientation="Horizontal"> <TextBlock Text="The parent StackPanel has " /> <TextBlock Text= "{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}, AncestorLevel=2}, Path=Orientation}" /> <TextBlock Text=" orientation" /> </StackPanel> </StackPanel> |
▶ ControlTemplate 정의 엘리먼트가 바인딩 소스인 경우 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="_pBorder" BorderThickness="2" BorderBrush="Black" CornerRadius="20"> <Border.Background> <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5"> <GradientStop Offset="0.0" Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color}" /> <GradientStop Offset="0.9" Color="White" /> </LinearGradientBrush> </Border.Background> <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" /> </Border> </ControlTemplate> |