728x90
728x170

RelativeSource 는 엘리먼트 트리 내에 조상 엘리먼트나 엘리먼트 그

자체를 참조하여 바인딩을 처리합니다.

 

아래 코드를 보면

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:src="clr-namespace:WpfApp"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <StackPanel>

        <StackPanel

            Orientation="Horizontal"

            HorizontalAlignment="Center">

            <TextBlock

                Text="{Binding RelativeSource={RelativeSource self}, Path=FontFamily}" />

        </StackPanel>

        <StackPanel

            Orientation="Horizontal"

            HorizontalAlignment="Center">

            <TextBlock

                Text= "{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Orientation}" />

        </StackPanel>

    </StackPanel>

</Window>

 

 

self 는 자기 자신을 가리키며 두번째는 AncestorType 으로 앨리먼트 트리에 StackPanel 을 가리키게 됩니다.

728x90
그리드형

'C# > WPF' 카테고리의 다른 글

[WPF] Style.Triggers 이용하여 Control 변화 주기  (0) 2020.07.10
[WPF] Style 사용하기  (0) 2020.07.10
[WPF] IValueConverter 사용하기  (0) 2020.07.10
[WPF] Custom FrameworkElement 사용하기  (0) 2020.07.10
[WPF] Binding  (0) 2020.07.09
Posted by kjun
,