본문 바로가기
C#/WPF

RelativeSource Binding

by Falto 2023. 3. 1.

RelativeSource를 이용하면 c# 코드 없이 순수 xaml로만 바인딩을 할 수 있다.

<Window x:Class="IamNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Height="450" Width="450" Background="#999999">
    <Grid>
        <Label Content="{Binding RelativeSource={RelativeSource AncestorLevel=1, AncestorType=Window}, Path=ActualWidth}"/>
    </Grid>
</Window>

실행 결과는 아래와 같다.

보다시피 Window의 ActualWidth가 Label의 Content에 바인딩이 됐다.

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

OneWay와 TwoWay  (0) 2023.03.01
Binding - UpdateSourceTrigger  (0) 2023.03.01
DataContext Binding  (0) 2023.03.01
[C# WPF] Binding  (0) 2023.02.18
[C# WPF] super mario 63의 cheat를 구현해 보자.  (0) 2023.02.03

댓글