본문 바로가기
C#/WPF

[C# WPF] Binding

by Falto 2023. 2. 18.

Binding이란? WPF에서 쓸 수 있는 기술이다. 쓰는 방법은 무척 간단하다.

<Window x:Class="WPFTEST.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"
        xmlns:local="clr-namespace:WPFTEST"
        mc:Ignorable="d"
        Title="{Binding ElementName=asdf, Path=ActualHeight}" Height="450" Width="800" Background="Black" x:Name="asdf">
</Window>

큰 따옴표 안에 {Binding}을 써 주기만 하면 끝이다.

Binding을 사용할 수 있는 가장 단순하고 쉬운 방법은 ElementName을 지정한 후 그 Element의 속성을 Path로 지정해주는 것이다. 실제로 저 프로그램을 실행해보면...

프로그램 창의 Title이 ActualWidth와 실시간으로 동기화되는 것을 볼 수 있다.

댓글